2017-10-19 268 views
0

我希望允許下拉列表顯示以前選擇的值,但現在已從下拉列表源中刪除。而不是顯示空白。下拉列表位於網格列中。如果選中的值不在源中,DropDownList將顯示空白值

網:

... 
columns.ForeignKey(p => p.CurrentCategory, @Model.LookupCategory, "CategoryName", "CategoryName").Width(160); 
... 

模板編輯器

@using System.Collections 

@(
Html.Kendo().DropDownListFor(m => m)  
     .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"]) 
     .ValuePrimitive(true)   
     .AutoWidth(true) 
) 

因此,在更詳細的解釋:該CurrentCategory列是文本列(而不是ID列),並且用戶可以從LookupCategory中找到的項目列表中進行選擇。但是,如果從LookupCategory中刪除某個項目,則應該在用戶已經爲CurrentCategory選擇該值的情況下顯示該值。

當前,如果一行包含不在LookupCategory列表中的CurrentCategory值,它將顯示爲空白。

也許我必須使用組合框呢?

回答

1

您可能會爲您的視圖模型添加另一個屬性AllCategory,其中包含LookupCategory和已刪除項目的聯合。 該屬性將被網格用來綁定菜單選項,而LookupCategory屬性將用作下拉源。

請參閱下面的內容在使用外鍵列模板時如何區分兩者。

columns.ForeignKey(p => p.CurrentCategory, Model.AllCategory, "CategoryName", "CategoryName") 
     .EditorViewData(new {lookupCategory = Model.LookupCategory}) 
.Width(160); 


@using System.Collections 

@(
Html.Kendo().DropDownListFor(m => m)  
     .BindTo((SelectList) ViewData["lookupCategory"])   
     .ValuePrimitive(true)   
     .AutoWidth(true) 
) 
+0

謝謝,這個工程。有沒有辦法避免使用ViewData並直接從模型填充? – Reafidy

+1

我不這麼認爲 –