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

发布于 2024-12-20 14:15:19 字数 332 浏览 3 评论 0原文

我是 WebForms 到 MVC 3 的新手,并且对 @Html.EditorFor() 帮助器方法有疑问。

我有一个强类型视图,它表示数据库中的数据,其中一种方法的类型为 bool?。我希望它显示为复选框,但它显示为带有“未设置”、“True”和“False”选项的下拉列表。

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

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

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

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".

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居者 2024-12-27 14:15:19

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

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

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

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

友谊不毕业 2024-12-27 14:15:19

基本上,ASP.NET MVC 有一些默认模板(您

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

这是布尔值的默认编辑器,您可以对其进行增强:

<%@ 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" }) %>
<% } %>

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

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.

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" }) %>
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文