pass parameter with grid

1 Answer 1144 Views
Grid
Mohammed
Top achievements
Rank 1
Mohammed asked on 28 May 2021, 07:25 AM

I need to pass parameter with grid to controller just i want to pass  

BusId , FlightId , griddata to controller 

 

this funtion send only griddata  i want to add BusId , FlightId to save it in one table

   function SaveData(e) {

        var List = []

        BusId: $("#BusId").val() form DropdownList (1) cascadeDrop

       FlightId: $("#Flights").val()  form DropdownList (2) cascadeDrop

       var griddata = $("#Grid").data("kendoGrid"); From Selected Grid

        var rows = griddata.select(); which i selected form grid
        rows.each(function (e) {
            var dataItem = griddata.dataItem(this);
            List.push(dataItem);
        });
       
           $.ajax({
                 type: "POST",
                 url: "@Url.Action("Create", "Employee")",
                 dataType: "text",
                 data: JSON.stringify(List),
                 contentType: "application/json; charset=utf-8",
                 processData: false,

               success: function (data) {

               },

               error: function (e) {

}

   viewModel

   public class EmpViewModel
    {
        public int BusId{ get; set; }

        public int FlightId{ get; set; }

       public List<Employee>AllEmployee{ get; set; }
    }

 

Controller

public IActionResult Create([FromBody] IEnumerable<Employee> empList)
        {

        }

                                                                  
Mohammed
Top achievements
Rank 1
commented on 28 May 2021, 07:42 PM

Any help please

1 Answer, 1 is accepted

Sort by
1
Stoyan
Telerik team
answered on 01 Jun 2021, 02:47 PM

Hi Mohammed,

The DataSource's CRUD methods  allow passing additional data to the Controllers via the .Data method as explained in this forum thread. The following steps modify the approach to more closely fit your scenario:

  1. Pass a string parameter to the DataSource's Data:
    .DataSource(dataSource => dataSource
                    .Ajax()
                    .Model(model => model.Id(p => p.ProductID))
                    .Read( read => read.Action("Grid_Read", "Home").Data("sendAdditionalData")
                    )
    )
  2. Define the sendAdditionalData function in a script
    1. Get the value of the DropDownList
    2. Get the selected row of the Grid
    3. Return an object that contains the data you want to pass to the Controller
          function sendAdditionalData() {
              var dropdown = $("#ddl").getKendoDropDownList();
              var ddlValue = dropdown.value();
      
              var grid = $("#grid").data("kendoGrid");
              var selection = grid.getSelectedData();
      
              return { dropdown: ddlValue, gridSelection: selection  }
          }
  3. Add the key values of the additional data returned by the client-side function as parameters in the JsonResult method of the Controller:
    public JsonResult Grid_Read([DataSourceRequest] DataSourceRequest request, string dropdown, string gridSelection)
            {
                //use dropdown and selection
               //define the data to be returned
    
                return Json(data.ToDataSourceResult(request));
            }

Please note that the parameters must be of the same type as the data passed by the client.

I am attaching a sample project that showcases the above.

Please let me know, if you have additional questions.

Regards,
Stoyan
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Mohammed
Top achievements
Rank 1
commented on 01 Jun 2021, 05:25 PM

thank a lot
Stoyan
Telerik team
commented on 03 Jun 2021, 12:27 PM

I am glad to be of help. Please don't hesitate to reach out should additional questions occur.
Tags
Grid
Asked by
Mohammed
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or