【问题标题】:Editor Template for Nullable Boolean Dropdown list not rendering可空布尔下拉列表的编辑器模板未呈现
【发布时间】:2016-03-18 07:17:10
【问题描述】:

我有一个名为 Critical 的属性,它是一个 Nullable Bool,例如“NULL, True, False”作为关键位 null 存储在表中。用户可以选择“Yes/No 或空白(将值存储为 NULL)

我在 Shared/EditorTemplates 中创建了一个名为 FriendlyBool.cshtml 的编辑器模板

@model bool? 
@using System.Web.Mvc   
@{   
    var selectList = new List<SelectListItem>();  
    selectList.Add(new SelectListItem { Text = "", Value = "" }); 
    selectList.Add(new SelectListItem { Text = "Yes", Value = "true", Selected = Model.HasValue && Model.Value }); 
    selectList.Add(new SelectListItem { Text = "No", Value = "false", Selected = Model.HasValue && !Model.Value });
 } 

在我看来,我称之为编辑器模板

 <div class="bodyContent">
        <span class="leftContent">
            @Html.Label("Critical")
        </span><span class="rightContent">
         @Html.EditorFor(model => model.critical, "FriendlyBool")
        </span>
    </div>

当我运行我的视图时,我看到默认下拉列表,其值为“未设置”、“真”和“假”。为什么我的编辑器模板不显示?

【问题讨论】:

  • 您的 EditorTemplate 除了创建 List&lt;SelectListItem&gt;(); 之外什么都不做 - 它不显示任何内容(不生成任何 html)
  • 它渲染的是默认的 SelectList 而不是编辑器模板中的那个
  • EditorTemplate 中根本没有要渲染的内容(只是创建一个新的SelectList 并不意味着它会被渲染)。至少模板需要包含@DropDownListFor(m =&gt; m, selectlist)
  • @StephenMuecke :) 明白了,现在渲染。还有一件事,当我提交表单时,MVC 验证会阻止提交表单,除非我从下拉列表中选择一个值。将空白值添加到下拉列表的全部原因是用户没有选择值并选择空白。如何防止 MVC 不进行下拉选择,它的高亮显示为红色。
  • 然后删除第一个SelectListItem并使用@DropDownListFor(m =&gt; m, selectlist, "")并确保该属性没有[Required]属性

标签: c# asp.net-mvc-3


【解决方案1】:

从 alireza 扩展答案以满足传递 htmlAttributes 的需求:

@model bool?
@{
    var selectList = new List<SelectListItem>
    {
        new SelectListItem {Text = "(Any)", Value = String.Empty, Selected = !Model.HasValue},
        new SelectListItem {Text = "Yes", Value = "true", Selected = Model.HasValue && Model.Value},
        new SelectListItem {Text = "No", Value = "false", Selected = Model.HasValue && !Model.Value}
    };
}
@Html.DropDownListFor(m => m, selectList, ViewData["htmlAttributes"])

用法:

@Html.EditorFor(model => model.critical, new
{
    htmlAttributes = new { @class = "variables-filter", disabled = "disabled" }
})

【讨论】:

    【解决方案2】:

    EditorTemplate 没有生成任何 html(只是创建了一个 SelectList)。将模板更改为

    @model bool? 
    @using System.Web.Mvc   
    @{   
        var selectList = new List<SelectListItem>();  
        selectList.Add(new SelectListItem { Text = "Yes", Value = "true" }); 
        selectList.Add(new SelectListItem { Text = "No", Value = "false" });
    } 
    @Html.DropDownListFor(m => m, selectList, "")
    

    同时确保您的属性没有[Required] 属性。

    附注

    1. 使用接受labelOptionDropDownListFor() 的重载 生成null 选项
    2. 无需设置SelectListItemSelected 属性。 您正在绑定到一个属性,因此如果值为null,则第一个 选项将被选中,或者如果它的true 第二个选项(“是”) 将被选中。

    【讨论】:

      【解决方案3】:

      如果你想从数据库中更新你的属性,使用这个:

      @using System.Web.Mvc
      @model bool?
      @{
      var selectList = new List<SelectListItem>();
      selectList.Add(new SelectListItem { Text = "", Value = String.Empty,Selected = !Model.HasValue });
      selectList.Add(new SelectListItem { Text = "Yes", Value = "true",Selected = Model.HasValue && Model.Value });
      selectList.Add(new SelectListItem { Text = "No", Value = "false", Selected = Model.HasValue && !Model.Value });}
      @Html.DropDownListFor(m => m, selectList)
      

      您也可以为您的财产使用[UIHint("FriendlyBool")] 属性来使用此模板

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多