@Html.EditorFor() 渲染 DropDownList 而不是 CheckBox
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用复选框辅助方法,@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".
基本上,ASP.NET MVC 有一些默认模板(您
如果您愿意,您可以添加自己的 EditorTemplate,ASP.NET MVC 将使用它而不是默认的。为此,您应该将文件“Boolean.{your-view-engine-extension}”(例如:“Boolean.aspx”)放入 ~/Views/ControllerName/EditorTemplates/ 或 >~/Views/Shared/EditorTemplates/ 并用您自己的功能覆盖它。
这是布尔值的默认编辑器,您可以对其进行增强:
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: