在 MVC 4 视图中唯一命名 Javascript Kendo 对象

Uniquely naming Javascript Kendo objects within MVC 4 view

本文关键字:Javascript Kendo 对象 一命 唯一 MVC 视图      更新时间:2023-09-26

我正在尝试在从同一控制器加载的同一部分视图中加载多个剑道网格。 显示的网格数量是动态的 - 这取决于用户的选择。

问题在于 HTML 页面中网格的所有实例化都显示相同的数据集,即对应于上次加载的网格的数据集。 我认为这是因为网格都具有相同的名称:

  @(Html.Kendo().Grid<RegistrationManagement.Models.Member>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.LastName);
        columns.Bound(p => p.FirstName);
        columns.ForeignKey(p => p.MemberStatusID,
                      (System.Collections.IEnumerable)ViewData["status"], "Value", "Text");
        columns.Bound(p => p.RegistrationYear);
        columns.Bound(p => p.StreetAddress1);
        columns.Bound(p => p.StreetAddress2);
        columns.Bound(p => p.State);
        columns.Bound(p => p.ZipCode);
        columns.Bound(p => p.Email);
        columns.Bound(p => p.PhoneNumber);
        columns.Command(command => { command.Custom("Edit2").Click("CustomEdit");
              command.Edit(); command.Destroy(); }).Width(160);
    })
            .ToolBar(toolbar => toolbar.Create())
        //    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Editable()
    .Groupable()
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Batch(true)
        .ServerOperation(true)
        .Events(events => events.Error("error_handler"))
        .Model(model =>
            {
                model.Id(p => p.Id);
                model.Field(p => p.MemberStatus).Editable(false);
            })
        .Read(read => read.Action("Grid_Member_Read", "Member", new { ClubID = @ViewBag.ClubID }))
        .Create(update => update.Action("Grid_Member_Create", "Member", new { ClubID = @ViewBag.ClubID }))
        .Update(update => update.Action("Grid_Member_Update", "Member"))
        .Destroy(update => update.Action("Grid_Member_Destroy", "Member"))
     )
)

所以这是两个问题之一:要么结构错误,我无法使用 AJAX 使用来自同一 MVC 控制器的不同数据集填充多个网格(不知道),要么我需要为网格指定唯一的名称。 我想采取第二条路径,希望通过基于 ViewBag 变量动态命名网格。 我试过这个:

.Name(Html.ViewBag.ID)

我在控制器中设置,但是当我运行它时,IE 告诉我:

Compiler Error Message: CS1977: Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
Source Error:
Line 3:  @(Html.Kendo().Grid<RegistrationManagement.Models.Member>()
Line 4:      .Name(Html.ViewBag.PrettyID)
Line 5:      .Columns(columns =>
Line 6:      {
Line 7:          columns.Bound(p => p.LastName);

我认为这是由于Html.ViewBag.PrettyID没有被解析。 如何为每个剑道网格创建唯一的名称?

谢谢!

你应该尝试把它强制转换为字符串:

.Name((String)Html.ViewBag.PrettyID)

如果你不关心实际值,你可以使用Guid.NewGuid()

.Name("grid" + Guid.NewGuid().ToString())