我有一个剃刀视图(Framework4.5,MVC5)和一个html输入type=checkbox,它的值等于模型布尔值,但它绑定的不是真或假,而是“值”。
这是我的代码:
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 />
}location.ThirdValue是布尔型属性,调试该属性就可以了。但是在超文本标记语言中,我得到的是value="value"而不是value="false"。
发生什么事了呢?
发布于 2013-11-26 03:06:09
查看此Answer
基本上,您希望使用HTML.CheckBoxFor(model => model.booleanProperty)
发布于 2013-11-26 03:00:27
执行此操作
<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)" />值未设置复选框是否选中,值具有不同的功能。例如,如果您将value设置为'test‘并选中该复选框,当您提交表单时,您将看到提交变量的真值为’test‘而不是true;
你可以用它做一些很酷的事情。例如,您的表单上有3个复选框。它们都具有相同的名称,但值不同。当您提交表单时,您得到的结果将是逗号分隔的字符串,其值为选中的复选框;
https://stackoverflow.com/questions/20200967
复制相似问题