从kendo Grid获取Checked Rows [Selected Rows]

时间:2014-11-06 12:04:21

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 kendo-ui kendo-grid

这是我的网格,当选中复选框时,我想将CheckBox Checked / Not Checked Value传递给Controller。在控制器模型中访问时,不会保留Checkbox值。你能指导我实现吗?

我的观点:

@using(@Html.BeginForm("RequisitionApply", "Requisition", FormMethod.Post, new               {           approvalReason = Model, @id = "addExpenseItem" }))
{
    @(Html.Kendo().Grid<ZCW.MVC.ViewModels.ManagerRequisitionSearchListViewModel>()
                 .Name("ManagerRequisitionApprovalGrid")
                  .DataSource(dataSource => dataSource
                                    .Ajax()
                                    .Events(e => e.RequestEnd("GetPaginationDisplay"))
                                    .PageSize(25)
                                                    .Read(read => read.Action("GetDetails", "List").Type(HttpVerbs.Get))
                      //.Update(update => update.Action("Apply", "List").Type(HttpVerbs.Post))
                                    .ServerOperation(true)
                                    )
                             .Columns(columns =>
                        {
                            //RequistionName(ID)
                            columns.Bound(p => p.FirstName).ClientTemplate("#=FirstName#<a href='" + Url.Action("GetResource", "List", new { NameID = "#=NameID#"}) + "'>(#=NameID#) </a>").Title(@Html.LocalizedText("ID")).HtmlAttributes(new { @class = "alignleft" });

                            //Location
                            columns.Bound(p => p.Location)
                                    .Title(@Html.LocalizedText("Location")).HtmlAttributes(new { @class = "alignleft" });


                            // Accept Header
                              columns.Template(p => p.Accept).Title("")
                                .HeaderTemplate("<i class='icon-ok gicn tooltips' data-original-title='Approve' data-placement='top'></i><br><div class='checker'><span><input id='selectallApprove' class='chkboxApprove' type='checkbox' onclick='ToggleChkBoxApprove(this.checked);' #= Accept ?  checked='checked' : '' # /></span></div>")
                                 .ClientTemplate("<span>  <input id='checkbox' onclick='grdChkBoxClick(this); ' class='chkboxApprove' type='checkbox' name='remember' /></span>")
                                   .HeaderHtmlAttributes(new { @class = "aligncenter" })
                                .HtmlAttributes(new { @class = "textaligncenter" });


                            //Decline Header
                            columns.Template(p => p.Decline).Title("")
                               .HeaderTemplate("<i class='icon-remove ricn tooltips' data-original-title='Decline' data-placement='top'></i><br><div class='checker'><span> <input id='selectallDecline'   class='chkboxDecline' type='checkbox' onclick='ToggleChkBoxDecline(this.checked);' #= Decline ?  checked='checked' : '' # /></span></div>")
                                .ClientTemplate("<span>  <input id='checkbox1' onclick='grdChkBoxClick(this); ' class='chkboxDecline' type='checkbox' name='remember'/></span>")
                                  .HeaderHtmlAttributes(new { @class = "aligncenter" })
                               .HtmlAttributes(new { @class = "textaligncenter" });
                        })
                                                                             .AutoBind(true)
                                                                                    .Sortable()
                                                                                    .EnableCustomBinding(true)
                    )

     <div class="pull-right">
                            <button type="submit" id="Apply" class="submitbtn" style="border: 0px none;"><i class="greenbtn"></i>@GlobalResourceHelper.GetGlobalResource(ResourceFiles.ButtonsResources, "BTN_APPLY")</button>
                        </div>
}

MyController:

 public ActionResult Apply(ResourceViewModel ApprovalList)
 {
     // I'm not getting Checkbox Checked or not in ApprovalList
  }

1 个答案:

答案 0 :(得分:0)

你必须这样做。

columns.Bound(x => x.IsChecked).ClientTemplate(
"<input name='IsChecked' class='chkBox' type='checkbox' 
  data-bind='checked: IsChecked' #= IsChecked ? checked='checked' : '' #/>");

        //check document ready 
    var grid = $("#Grid").data("kendoGrid");
        grid.tbody.on("change", ".chkBox", function (e) {

            var row = $(e.target).closest("tr");
            var item = grid.dataItem(row);                
            //item.set("IsChecked", $(e.target).is(":checked") ? 1 : 0);
            kendo.bind(this,item);//this line or above line will handle this
        });   

使用ajax调用发布网格并发送网格数据。

   var gridData = $("#Grid").data("kendoGrid");
       //AJAX etc...
        data: JSON.stringify(gridData.dataSource.view()),