2015-02-23 17 views
0

我有一個非常簡單的產品,有一個用戶表中的編輯器。劍道格應顯示鏈接表的價值

整個事情都映射在edmx中。

在我的獲取方法我有一些代碼:

public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request) 
    { 
     using (var context = new ProductModel()) 
     { 
      var products = from row in context.Products select new { row.Id, row.Title, row.EditorId, row.Editor.Name }; 
      var result = products.ToDataSourceResult(request); 
      //Debug: 
      //var s = new JavaScriptSerializer().Serialize(Json(result, JsonRequestBehavior.AllowGet)); 
      return Json(result, JsonRequestBehavior.AllowGet); 
     } 
    } 

不幸的是,我可以看到Editor.Name被序列化到「姓名」。

在CSHTML然後我有我的網

@(Html.Kendo().Grid<Product>() 
     .Name("grid") 
     .DataSource(dataSource => dataSource 
      .Ajax() 
      .Model(model => model.Id(c => c.Id)) 
      .Read(read => read.Action("Products_Read", "Product")) 
      .Update(update => update.Action("Products_Update", "Product")) 
    ) 
     .Columns(product => 
     { 
      product.Bound(p => p.Id).Hidden(true); 
      product.Bound(p => p.Title).Title("Title"); 
      product.Template(p => p.Editor.Name).Title("Editor");//<-- HERE 

任何事都在網格中,我認爲我有這麼電網知道從哪裏獲取的值來裝點東西。

否則我可以做一個viewmodel,但是然後我會失去很多功能的構建。

下一步將是我編輯產品時的下拉菜單。我的希望是有人可以提供一個鏈接到一個開箱即用的地方。

+0

當你嘗試一下本作會發生什麼事您檢索: '變種產品= context.Products.ToList();'。我知道這可能會檢索超過你想要的,但我相信這個問題是與你的'新聲明' – 2015-02-23 14:32:50

+0

ToList工作。舉例來說,Json的第一部分看起來像這樣:### {「ContentEncoding」:null,「ContentType」:null,「Data」:{「Data」:{「Id」:1,「標題「:」Book1「,」EditorId「:218,」Name「:」Bob「} ###請注意Bob的名字有多惱人。在舊的.Net中,會有東西表明這是嵌套實體。我不能做一個context.Products.ToList,因爲它太多了,我必須減少select,否則我會在序列化中得到一個循環 – 2015-02-23 15:05:47

回答

1

你需要設置模板這樣

.Columns(product => 
    { 
     product.Bound(p => p.Editor.Name).Template(@<text> 
      <strong>@item.Name</strong> 
     </text>).Title("Editor"); 
    }) 

Refrence

+0

@ item.Name不起作用,因爲我的產品沒有名稱。我認爲一個視圖模型可能是最容易的。 – 2015-02-24 08:41:34