【问题标题】:CheckBox and id value to actionresult in kendo ui with ajax binding使用 ajax 绑定在 kendo ui 中操作的 CheckBox 和 id 值
【发布时间】:2014-06-16 20:23:00
【问题描述】:

我正在开发一个带有 kendo UI 网格的 MVC。其中我有网格和一个按钮。网格包含复选框。当我们点击按钮复选框值(真/假)和记录ID应该去​​行动结果。

        using (Html.BeginForm("Update", "Model"))
        {


            Html.Kendo().Grid(Model.Users)
                        .Name("grid")

                        .Columns(columns =>
                               {
                                   columns.Bound(o => o.UserName).Width("300px");
                                   columns.Bound(o => o.IsLicensed).Template(o =>
                                   {%><%: Html.CheckBox("license",o.IsLicensed)%> <%: Html.Hidden("id", o.UserId) %><%}).Width("200px");
                               })
                               .Resizable(resize => resize.Columns(true))
                               .Sortable(sorting => sorting.Enabled(true))
                               .Filterable(f => f.Enabled(true))
                               .Pageable(pageable => pageable
                                                        .Refresh(true)
                                                        .PageSizes(new int[] {10, 20, 50, 100})
                                                        .ButtonCount(5))             
                               .Render();           

%>

            <p>
                <input class="k-primary k-button" type="submit" value="Save" name="selectedValues"/>
            </p>
        <%
            Html.EndForm();
        }
        %>

    </div>
    <% });

我的操作结果如下所示

public ActionResult Update(string[] license, string[] id)

现在我在这里使用 ajax 绑定并提供数据源。所以我不能使用模板。我们需要使用 ClientTemplate。 ClientTemplate 代码更改如下所示。

                             .Columns(columns =>
                               {

                                   columns.Bound(o => o.UserName).Width("300px");
                                   columns.Bound(o => o.IsLicensed).Width("300px").ClientTemplate("<input type='checkbox' name='license' ${ IsLicensed == true ? checked='checked' : ''} enabled />" + "<input type='hidden' name='id' value='#= UserId#' />");
                               })
                               .DataSource(datasource => datasource.Ajax().Read(read => read.Action("Customers_Read", "UsersLicensing")).PageSize(10))
                               .Render();

在以前的情况下,当调用 Update actionresult 时,两个参数都是 10 的数组,因为 pagesize 是 10,我可以更新数据库。但在这种情况下(使用 ajax 和 clientTemplate)id 参数作为 10 个项目的数组出现,但许可证参数取决于所选复选框的数量(如果选择了 3 个复选框,则许可证包含 3 个元素)。所以我无法进行一对一的映射。

有人可以帮我解决这个问题或提出更好的主意吗?

【问题讨论】:

    标签: ajax asp.net-mvc checkbox kendo-ui kendo-grid


    【解决方案1】:

    试试这个....为我工作

    columns.Template(@<text></text>).
    ClientTemplate("<input type='checkbox' class='chkbx'  onclick='chkRoleChkBox(this)' 
    value='#=RoleId#'  # if (DefaultRoleflag == 'Y') { # disabled='disabled' # } #/>").Width(30);
    

    【讨论】:

      【解决方案2】:

      您应该能够像这样更改您的客户端模板:

      .ClientTemplate("<input type='checkbox' name='license' ${ IsLicensed == true ? checked='checked' : ''} enabled value='#= UserId#' />")
      

      UserId直接放入复选框的值中。然后,当您发布时,您应该获取已检查项目的值(即 UserId)数组,而不是获取 "true" 的数组。

      更新

      如果您希望能够同时获取选中列表和未选中列表,则可以将隐藏字段添加回ClientTemplate

      .ClientTemplate(
          "<input type='checkbox' name='license' ${ IsLicensed == true ? checked='checked' : ''} enabled value='#= UserId#' />" + 
          "<input type='hidden' name='id' value='#= UserId#' />"
      )
      

      然后,当您发布时,您将在string[] id 中获得一个包含所有 ID 的数组,并在 string[] license 中获得一个已检查 ID 的数组。然后要取消选中,您可以使用 System.Linq 命名空间并使用 Enumerable.Except

      public ActionResult Update(string[] license, string[] id)
      {
          //....
          var unchecked = id.Except(license).ToArray();
          //...
      }
      

      【讨论】:

      • 如果我可以获得特定的 id 数组,则此解决方案可以工作。因此我可以为特定页面生成一对一。你有没有关于它的id?
      • 我明白了。当您发布时,您只会获得选中的值,但您需要知道哪些已选中,哪些未选中。在这种情况下,请留下我的代码并重新添加隐藏的输入。然后,当您发布时,您将拥有一个包含所有 ID 的数组和一组已检查的 ID。那么简单的事情就是做一个 LINQ except 来获取未经检查的 ID。我会更新我的答案。
      • 更新评论后,我正在使用它并找到相同的解决方案。感谢您对使用 Enumerable.Except 的建议。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      相关资源
      最近更新 更多