在kendo Grid MVC中显示两个值外键

时间:2016-06-13 11:24:01

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

我很难解决这个问题。我只想显示两个值,例如在columns.Foreignkey中。在同一列上显示名称和ID。我该怎么做呢?请回复

@(Html.Kendo().Grid<Project_Test.Models.Workcenter>()

    .Name("grid")
    .Columns(columns =>
            {

  columns.Bound(p => p.WorkcenterCode).Width(80).Title("Workcentercode").ClientTemplate("");

 columns.Bound(p => p.WorkCenterSection).Width(84).Title("WorkcenterSection");

columns.ForeignKey(p => p.WorkcenterCategory, (System.Collections.IEnumerable)ViewBag.WorkcenterCategory, "WorkcenterCategoryID", "WorkcenterCategoryName")
         .Title("Category").Width(200);

            //command.Custom("Details").Click("showDetails").HtmlAttributes(new { @class = "k-font-icon  k-i-insert-unordered-list " });
            command.Custom(" ").Click("showDetails").HtmlAttributes(new { @class = "btn btn-default btn-xs  glyphicon glyphicon-list-alt" });
        }).Width(230);
    })
       // .Events(e => e.DataBound("onDataBound"))
  .ToolBar(toolbar => toolbar.Create())
     .Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("Workcent").DisplayDeleteConfirmation("Are you sure to delete this Workcenter"))
    .Sortable()
         .Pageable(pageable => pageable
                    .Refresh(true)
                    .PageSizes(true)
                         .Messages(messages => messages.Refresh("Click to refresh"))
                    .ButtonCount(8))
                        .Groupable()

    .HtmlAttributes(new { style = "height:580px;" })
        .Resizable(resize => resize.Columns(true))
        .Reorderable(reorder => reorder.Columns(true))
               .Selectable()
             .Scrollable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Events(events => events.Error("error_handler"))
            .Model(model =>
            {
                model.Id(p => p.WorkcenterID);
                model.Field(p => p.WorkcenterID).Editable(true);
                model.Field(p => p.UpdatedBy).DefaultValue(User.Identity.Name);
                model.Field(p => p.CreatedBy).DefaultValue(User.Identity.Name);

            })
          .Create(update => update.Action("Employee_Create", "Home"))
         .Read(read => read.Action("List", "Home"))
  .Update(update => update.Action("Products_Update", "Home"))
            .Destroy(update => update.Action("adress_Destroy", "Home"))

    )

1 个答案:

答案 0 :(得分:0)

ForeignKey列只显示与绑定列表中相应的WorkcenterCategoryID关联的WorkcenterCategoryName(ViewBag.WorkcenterCategory)。

只需使ViewBag.WorkcenterCategory中每个元素的WorkcenterCategoryName包含您实际想要显示的文本,即

"IDOfSelectedItem (TextOfSelectedItem)"

而不仅仅是

"TextOfSelectedItem"

编辑更具体:

我假设您的服务器操作正在填充ViewBag.WorkcenterCategory,例如:

ViewBag.WorkcenterCategory = GetWorkcenterCategories()
    .Select(x => new
    {
        WorkcenterID = x.ID,
        WorkcenterName = x.Name
    });

让我们说这会返回列表:

{ WorkcenterID = 1, WorkcenterName = "One" },
{ WorkcenterID = 2, WorkcenterName = "Two" }

然后,ForeignKey列将特定键映射到绑定到它的列表中的相应项(ViewBag.WorkcenterCategory),并且将显示与该键关联的文本值,WorkcenterName字段中包含的任何内容。 即值1将显示&#34; One&#34;在网格中。

如果要显示的不仅仅是Workcenter的名称,请将ViewBag.WorkcenterCategory列表中对象的WorkcenterName设置为您要显示的文本,即:

ViewBag.WorkcenterCategory = GetWorkcenterCategories()
    .Select(x => new
    {
        WorkcenterID = x.ID,
        WorkcenterName = x.ID + " (" + x.Name + ")"
    });

这将返回列表:

{ WorkcenterID = 1, WorkcenterName = "1 (One)" },
{ WorkcenterID = 2, WorkcenterName = "2 (Two)" }

现在将显示值1&#34; 1(一)&#34;而不只是&#34; One&#34;。