alenan2013 alenan2013 - 10 days ago 6
C# Question

Getting checked rows from Telerik Kendo Grid in MVC Controller

I have a Telerik Kendo Grid with a checked column:

@(Html.Kendo().Grid<RunSummary>()
.Name("CheckedPatients")
.ToolBar(toolBar =>
toolBar.Custom()
.Text("Export To PDF")
.HtmlAttributes(new { id = "export" })
.Url(Url.Action("Export", "PatientReport", new { page = 1, pageSize = "~", filter = "~", sort = "~" })))

.DataSource(datasource => datasource
.Ajax().PageSize(25)
.Sort(sort => sort.Add("UniqueId").Ascending())
.Read(read => read.Action("GetRunSummaries", "PatientReport")))
.Columns(columns =>
{

columns.Bound(c => c.UniqueId).Title(ELSORegistry.Resources.Views.Home.HomeStrings.UniqueId)
.ClientTemplate("<input type='checkbox' unlock='true' class='primaryBox' id='#= UniqueId #' />");
columns.Bound(c => c.RunNo).Title(SharedStrings.Run);
columns.Bound(c => c.Birthdate).Title(SharedStrings.Birthdate).Format("{0:g}").Filterable(true);

//columns.Bound(c => c.Age).Title(SharedStrings.Age).ClientTemplate("#= formatAge(Age)#");
columns.Bound(c => c.TimeOn).Title(PatientStrings.TimeOn)
.Format("{0:g}")
.Filterable(true);
columns.Bound(c => c.TimeOff).Title(PatientStrings.TimeOff)
.Format("{0:g}")
.Filterable(true);
columns.Bound(c => c.DischargedAlive).Title(SharedStrings.Age).Filterable(true);
}
)
.Pageable(p => p.PageSizes(new[] {10, 25, 50, 100}))
.Sortable()
.Filterable( )
.Events( e => e.FilterMenuInit("FilterMenuFunc") ) // apply x [closing box] on pop up filter box
)


I have to get checked rows in an action of the controller. Please help how to do this? Thank you in advance

Answer

I finally found the solution. At first, collect all checked boxes to an array:

$(document).on('change', '.primaryBox:checkbox', function (e) {
        var isChecked = $(this).is(':checked');
        if (isChecked) {
            checkedArray.push($(this).val());
        } else {
            checkedArray.remove($(this).val());

        }
    });

Then, send the array to the controller using Ajax:

$.ajax({
            type: "POST",
            url: "/PatientReport/ExportToPDFStoredProcedures",
            dataType: "json",
            traditional: true,
            data: { uniqueIds: checkedArray },
            success: function (data) {
                if (data.success) {
                     //do something
                                   } 
           else {
                    //do something else;
                }
...and finally in the controller make an action that has uniqueIds as argument:
public ActionResult ExportToPDF(List<String> uniqueIds)
        {  
//do something with UniqueIds
}
                },
Comments