.NET MVC Html.CheckBoxFor的正确用法 [英] Proper usage of .net MVC Html.CheckBoxFor
问题描述
所有我想知道的是 Html.CheckBoxFor
HTML在ASP.NET MVC帮手正确的语法。
我试图完成是用一个ID值进行初始确认该复选框,所以我可以引用它的控制器,看它是否仍然选中。
下面会是正确的语法?
@foreach(在Model.Templates VAR项)
{
&所述; TD>
@ Html.CheckBoxFor(型号=>真,item.TemplateId)
@ Html.LabelFor(型号=> item.TemplateName)
< / TD>
}
这是不正确的语法
第一个参数是不会复选框值,而是查看型号为复选框因此绑定:
@ Html.CheckBoxFor(M = GT; m.SomeBooleanProperty,新{@checked =选中});
第一个参数必须在模型中确定一个布尔属性(这是一个前pression不是一个匿名方法返回一个值)和第二属性定义的附加的HTML元素的属性。我不是100%肯定的是,上述属性将首先检查你的复选框,但你可以试试。但要注意。尽管它可能工作,你可能以后的问题,装一个有效的模型数据和特定属性时,设置为假
。
的正确方法
虽然我的适当的建议的将是提供初始化模式与初始化为特定布尔属性,您认为真正
。
属性类型
根据Asp.net MVC 的HtmlHelper
扩展方法和内部工作,复选框需要绑定到布尔值,而不是整数,似乎什么,你想要做的事。在这种情况下,一个隐藏字段可以存储 ID
。
其他佣工
有,你可以用它来获取有关复选框,价值观和行为更大的灵活性,当然其他的辅助方法:
@ Html.CheckBox(templateId,新的{值= item.TemplateID,@checked =真});
的 注意:的
检查
的是一个HTML元素布尔属性,而不是一个值属性,这意味着你可以任意值分配给它。正确的HTML语法不包括任何任务,但没有提供与未定义的属性匿名C#对象将呈现为一个HTML元素属性的方式。的
块引用>All I want to know is the proper syntax for the
Html.CheckBoxFor
HTML helper in ASP.NET MVC.What I'm trying to accomplish is for the check-box to be initially checked with an ID value so I can reference it in the Controller to see if it's still checked or not.
Would below be the proper syntax?
@foreach (var item in Model.Templates) { <td> @Html.CheckBoxFor(model => true, item.TemplateId) @Html.LabelFor(model => item.TemplateName) </td> }
解决方案That isn't the proper syntax
The first parameter is not checkbox value but rather view model binding for the checkbox hence:
@Html.CheckBoxFor(m => m.SomeBooleanProperty, new { @checked = "checked" });
The first parameter must identify a boolean property within your model (it's an expression not an anonymous method returning a value) and second property defines any additional HTML element attributes. I'm not 100% sure that the above attribute will initially check your checkbox, but you can try. But beware. Even though it may work you may have issues later on, when loading a valid model data and that particular property is set to
false
.The correct way
Although my proper suggestion would be to provide initialized model to your view with that particular boolean property initialized to
true
.Property types
As per Asp.net MVC
HtmlHelper
extension methods and inner working, checkboxes need to bind to boolean values and not integers what seems that you'd like to do. In that case a hidden field could store theid
.Other helpers
There are of course other helper methods that you can use to get greater flexibility about checkbox values and behaviour:
@Html.CheckBox("templateId", new { value = item.TemplateID, @checked = true });
Note:
checked
is an HTML element boolean property and not a value attribute which means that you can assign any value to it. The correct HTML syntax doesn't include any assignments, but there's no way of providing an anonymous C# object with undefined property that would render as an HTML element property.
这篇关于.NET MVC Html.CheckBoxFor的正确用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!