@Html.EditorFor()渲染DropDownList而不是CheckBox

时间:2022-09-26 20:59:10

I'm new from WebForms to MVC 3 and have an issue with the @Html.EditorFor() helper method.

我是从WebForms到MVC 3的新手,并且遇到了@ Html.EditorFor()辅助方法的问题。

I have a strongly typed view that represents data from a database, and one of the methods is of type bool?. I'd like this to appear as a checkbox, but instead it appears as a dropdownlist with the options "Not Set", "True" and "False".

我有一个强类型视图,表示数据库中的数据,其中一个方法是bool?类型。我希望它显示为一个复选框,但它显示为一个下拉列表,其中包含选项“未设置”,“真实”和“假”。

What is the simplest way to covert this to a regular checkbox?

将此转换为常规复选框的最简单方法是什么?

I understand that I could change the data type to a plain old bool, but this is a large EF entity I'm using and it seems a pain to have to recreate the entire class just for this. I also realize I'll lose the ability to track the "not set" state, but showing a simple checkbox is more important to me.

我知道我可以将数据类型更改为普通的bool,但这是我正在使用的大型EF实体,并且为此需要重新创建整个类似乎很痛苦。我也意识到我将失去跟踪“未设置”状态的能力,但显示一个简单的复选框对我来说更重要。

2 个解决方案

#1


9  

Use the checkbox helper method instead, @Html.CheckBoxFor()

使用复选框辅助方法,@ Html.CheckBoxFor()

It's rendering a drop down list as a check box wouldn't be able to provide the value "not set".

它呈现下拉列表,因为复选框无法提供“未设置”值。

#2


6  

Basically, ASP.NET MVC has some default templates (you can read that here).

基本上,ASP.NET MVC有一些默认模板(你可以在这里阅读)。

If you wish, you could add your own EditorTemplate and ASP.NET MVC will use it instead of default. For this you should place a file 'Boolean.{your-view-engine-extension}' (ex.: 'Boolean.aspx') into either ~/Views/ControllerName/EditorTemplates/ or ~/Views/Shared/EditorTemplates/ and override it with your own functionality.

如果您愿意,可以添加自己的EditorTemplate,ASP.NET MVC将使用它而不是默认值。为此,您应该将文件'Boolean。{your-view-engine-extension}'(例如:'Boolean.aspx')放入〜/ Views / ControllerName / EditorTemplates /或〜/ Views / Shared / EditorTemplates /用你自己的功能覆盖它。

Here is the default editor for Boolean, which can be enhanced by you:

这是布尔值的默认编辑器,可以由您增强:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private List<SelectListItem> TriStateValues {
        get {
            return new List<SelectListItem> {
                new SelectListItem { Text = "Not Set", Value = String.Empty, Selected = !Value.HasValue },
                new SelectListItem { Text = "True", Value = "true", Selected = Value.HasValue && Value.Value },
                new SelectListItem { Text = "False", Value = "false", Selected = Value.HasValue && !Value.Value },
            };
        }
    }
    private bool? Value {
        get {
            if (ViewData.Model == null) {
                return null;
            }
            return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
        }
    }
</script>
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" }) %>
<% } else { %>
    <%= Html.CheckBox("", Value ?? false, new { @class = "check-box" }) %>
<% } %>

#1


9  

Use the checkbox helper method instead, @Html.CheckBoxFor()

使用复选框辅助方法,@ Html.CheckBoxFor()

It's rendering a drop down list as a check box wouldn't be able to provide the value "not set".

它呈现下拉列表,因为复选框无法提供“未设置”值。

#2


6  

Basically, ASP.NET MVC has some default templates (you can read that here).

基本上,ASP.NET MVC有一些默认模板(你可以在这里阅读)。

If you wish, you could add your own EditorTemplate and ASP.NET MVC will use it instead of default. For this you should place a file 'Boolean.{your-view-engine-extension}' (ex.: 'Boolean.aspx') into either ~/Views/ControllerName/EditorTemplates/ or ~/Views/Shared/EditorTemplates/ and override it with your own functionality.

如果您愿意,可以添加自己的EditorTemplate,ASP.NET MVC将使用它而不是默认值。为此,您应该将文件'Boolean。{your-view-engine-extension}'(例如:'Boolean.aspx')放入〜/ Views / ControllerName / EditorTemplates /或〜/ Views / Shared / EditorTemplates /用你自己的功能覆盖它。

Here is the default editor for Boolean, which can be enhanced by you:

这是布尔值的默认编辑器,可以由您增强:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<script runat="server">
    private List<SelectListItem> TriStateValues {
        get {
            return new List<SelectListItem> {
                new SelectListItem { Text = "Not Set", Value = String.Empty, Selected = !Value.HasValue },
                new SelectListItem { Text = "True", Value = "true", Selected = Value.HasValue && Value.Value },
                new SelectListItem { Text = "False", Value = "false", Selected = Value.HasValue && !Value.Value },
            };
        }
    }
    private bool? Value {
        get {
            if (ViewData.Model == null) {
                return null;
            }
            return Convert.ToBoolean(ViewData.Model, System.Globalization.CultureInfo.InvariantCulture);
        }
    }
</script>
<% if (ViewData.ModelMetadata.IsNullableValueType) { %>
    <%= Html.DropDownList("", TriStateValues, new { @class = "list-box tri-state" }) %>
<% } else { %>
    <%= Html.CheckBox("", Value ?? false, new { @class = "check-box" }) %>
<% } %>