[英]Kendo UI Grid - Display foreign key value instead of ID
網格使用包含外鍵值的下拉列表。 現在,我可以選擇一個項目,但是只能看到相應的項目ID,而不是網格中的名稱。
我嘗試以下的步驟這篇文章 ,但網格行中的所有我看到的是“未定義”。 有任何想法嗎?
順便說一下,我正在使用Kendo UI的開源版本,其中所有內容都在JavaScript中。
您需要使用模板。
在模型中,還有一個與名稱相關的字段(編輯時需要將其設置為正確的值),然后使用類似“#= name#
首先像這樣創建您的列:
columns.Bound(x => x.ForeignKey).ClientTemplate("#=Name#");
然后在控制器的Update方法中,將視圖模型上的Name屬性設置為所需的名稱。
viewModel.Name = GetName(viewModel.ForeignKey);
return this.Json(new[] { viewModel }.ToDataSourceResult(request, this.ModelState));
編輯2:為了以javascript模式構建網格,您將需要定義以下列:
{ field: "ForeignKey", title: "Foreign Key", template: '#=Name#', editor: myEditor}
然后像這樣定義編輯器:
function myEditor(container, options) {
$('<input required data-text-field="ForeignKeyName" data-value-field="ForeignKey" data-bind="value:' + options.field + '"/>')
.appendTo(container)
.kendoDropDownList({
autoBind: false,
dataSource: myDataSource
});
}
您可能仍然需要設置該值,但是,如果要在服務器端執行此操作,則可以使用上面提到的方法,如果要在客戶端執行此操作,則需要處理網格保存事件並設置該值。數據源中的值。
希望這可以幫助。
我最終向Telerik尋求支持,此JS Bin中顯示了有效的解決方案。
這是MVC版本,但是在這里,您不需要模板。 看到:
http://decisivedata.net/kendo-ui-grid-and-entity-framework-code-first-foreign-key-fields/
假設您要獲取產品名稱,而不要在“訂單”表上使用ProductID。 您將使用類似以下的列:
columns.ForeignKey(c => c.ProductID, (IEnumerable)ViewData["Products"], dataFieldText: "ProductName", dataFieldValue: "ProductID");
並確保您的模型在Orders模型中具有外鍵:
[ForeignKey("ProductID")]
public Product FKProduct { get; set; }
然后您更新控制器:
public class HomeController : Controller {
private NorthwindRepository northwindRepository = new NorthwindRepository();
public ActionResult Index()
{
PopulateProducts();
return View(northwindRepository.OrderDetails);
}
private void PopulateProducts()
{
ViewData["Products"] = northwindRepository.Products;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.