1

I'm trying to load multiple Kendo grids within the same partial view loaded from the same controller. The number of grids displayed is dynamic- it depends upon user selection.

The problem is that all instantiations of the grid within the HTML page are displaying the same dataset, the one corresponding to the last grid loaded. I think that this is because the grids all have the same name:

  @(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"))
     )
)

So it's one of two problems: either something's structurally wrong and I can't populate multiple grids with distinct datasets from the same MVC controller using AJAX (don't know), or I need to give the grids unique names. I'd like to take the second path, hopefully by naming the grids dynamically based upon a ViewBag variable. I've tried this:

.Name(Html.ViewBag.ID)

which I set in the controller, but when I run that, IE tells me:

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);

which I think is due to the Html.ViewBag.PrettyID not being parsed. How can I create a unique name for each Kendo grid?

Thanks!

4

1 回答 1

0

您应该尝试将其转换为字符串:

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

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

.Name("grid" + Guid.NewGuid().ToString())
于 2013-09-05T19:11:12.117 回答