【问题标题】:DropDownList Displays Blank Value if Selected Value Not in Source如果所选值不在源中,则 DropDownList 显示空白值
【发布时间】:2017-10-19 02:45:18
【问题描述】:

我希望允许下拉列表显示以前选择但现在已从下拉列表源中删除的值。而不是显示空白。下拉列表位于网格列中。

网格:

...
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 选择了该值的情况下。

目前,如果某行包含的 CurrentCategory 值不在 LookupCategory 列表中,则会显示为空白。

也许我必须改用组合框?

【问题讨论】:

    标签: c# asp.net asp.net-core kendo-ui telerik


    【解决方案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)
    )
    

    【讨论】:

    • 谢谢,这行得通。有没有办法避免使用 ViewData 并直接从模型中填充?
    • 我不这么认为
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多