2016-06-13 49 views
0

我很厌烦解决这个问题。我只想显示两个值,例如在columns.Foreignkey中。在相同列上显示名称和ID。我该怎么做呢?请回复在剑道中显示两个值外键Grid MVC

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

    ) 

回答

0

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

只是要在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的值将在网格中显示“One”。

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

如果要显示比对工作中心的只是名字更多,然后在ViewBag.WorkcenterCategory列表中你希望显示的文本,也就是设置对象的WorkcenterName这将返回列表:

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

而值1现在显示“1(一)”而不是“一”。

+0

我不明白是什么意思“TextOfSelectedItem”我只想在Grid中插入两个值FK我该怎么做,请详细解释 – Simo