如果选中的值不在源中,DropDownList将显示空白值

问题描述:

我希望允许下拉列表显示以前选择的值,但现在已从下拉列表源中删除。而不是显示空白。下拉列表位于网格列中。如果选中的值不在源中,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值,它将显示为空白。

也许我必须使用组合框呢?

您可能会为您的视图模型添加另一个属性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

我不这么认为 –