C# MVC 5 Razor视图未将布尔绑定到输入
我有一个razor视图(Framework4.5,MVC5)和一个html输入类型=复选框,其值等于一个模型布尔值,但它绑定的是“值”,而不是true或false 这是我的代码:C# MVC 5 Razor视图未将布尔绑定到输入,c#,html,asp.net-mvc,asp.net-mvc-4,razor,C#,Html,Asp.net Mvc,Asp.net Mvc 4,Razor,我有一个razor视图(Framework4.5,MVC5)和一个html输入类型=复选框,其值等于一个模型布尔值,但它绑定的是“值”,而不是true或false 这是我的代码: for (int index = 0; index < Model.Item1.Locations.Length; index++) { WSTupleOfInt32StringStringBoolean location = Model.Item1.Locations[i
for (int index = 0; index < Model.Item1.Locations.Length; index++)
{
WSTupleOfInt32StringStringBoolean location = Model.Item1.Locations[index];
<div class="editor-field">
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" value="@location.ThirdValue" name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
</div>
<div class="editor-label">
<label for="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)">@location.FirstValue</label>
@if (!string.IsNullOrEmpty(location.SecondValue))
{
<a href="@string.Format("//maps.google.com/?q={0}", location.SecondValue)" target="_blank"><img alt="@location.SecondValue" src="@string.Format("//maps.google.com/maps/api/staticmap?center={0}&markers=color:blue|{0}&zoom=15&size=64x64&sensor=false", location.SecondValue)" /></a>
}
</div><br />
}
for(int index=0;index
}
location.ThirdValue
是布尔属性,调试该属性很好。。但是在HTML中,我得到的是value=“value”
,而不是value=“false”
发生了什么事?这样做
<input id="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].Key", index, Model.Item2)" type="checkbox" @(location.ThirdValue ? "checked" : "") name="@string.Format("presentation.ServiceInfoes[{1}].Locations[{0}].ThirdValue", index, Model.Item2)" />
该值未设置要检查或不检查的复选框,该值具有不同的功能。例如,如果您将值设置为“test”并选中复选框,当您提交表单时,您将看到提交变量的值将不是true而是“test”
你可以用它做一些很酷的事情。例如,表单上有3个复选框。它们都有相同的名称,但值不同。提交表单时,得到的结果将是逗号分隔的字符串,带有复选框的值 看到了吗
基本上,您希望使用
HTML.CheckBoxFor(model=>model.booleanProperty)
在我的模型上,我有公共bool NewContact{get;'internal'set;},这使得即使使用CheckBoxFor,“NewContact”也始终为false。移除内部组件,这样可以工作。向上投票。