所以我从数字机构获得了 html,我需要将其转换为 MVC。据我所知, Html.CheckboxFor(..) 创建了一个额外的隐藏输入字段,以将未选择复选框捕获为 false。
但是在我的页面上我无法选择我的复选框。如果我删除<input type="hidden....>
在 Firebug 中我可以选择该复选框。 (就像我无法单击复选框并使其在视觉上被选中一样)
生成的html是:
(提供给我的html)
<div class="terms well">
<div class="checkbox">
<label class="checkbox-inline">
<input type="checkbox" id="authorize" name="authorize">
<span class="checkbox-display"><span class="checkbox-display-inner fa fa-check"></span></span>
<span class="checkbox-label">I accept full responsibility for the information I provide on this form.</span>
</label>
</div>
</div>
(由 html 助手生成的 html - 我没有费心尝试使用标签,因为我不确定样式是否仍然有效)。
<div class="terms well">
<div class="checkbox">
<label class="checkbox-inline">
<input type="checkbox" value="true" name="AuthorisePayment" id="AuthorisePayment"><input type="hidden" value="false" name="AuthorisePayment">
<span class="checkbox-display"><span class="checkbox-display-inner fa fa-check"></span></span>
<span class="checkbox-label terms-credit" style="display: inline-block;">I accept full responsibility for information and authorisations I provide on this form.</span>
</label>
</div>
</div>
有人有什么想法吗?我并不百分百确定要在这篇文章中包含哪些内容来帮助找到答案。
Razor View 包含:
<div class="terms well">
<div class="checkbox">
<label class="checkbox-inline">
@Html.CheckBoxFor(model => model.AuthorisePayment)
<span class="checkbox-display"><span class="checkbox-display-inner fa fa-check"></span></span>
<span class="checkbox-label terms-credit">@Editable(x => x.PaymentTemplate.CreditCardTerms)</span>
</label>
</div>
</div>
我可以从 Action Controller 中的表单获取值,但我不明白为什么模型绑定(bind)方式不起作用。
最佳答案
我也遇到了同样的问题。 Checkboxfor 从模型中得名。 问题是:
当 2 个不同的模型具有相同的属性时, 并且在同一页面上都有一个复选框, 出现所描述的问题。
在我的例子中,组织模型 (1) 和联系人模型 (2) 具有相同的属性:
公共(public) bool IsActive {获取;放; }
解决方案 就我而言,给他们一个唯一的 ID:
(1): @Html.CheckBoxFor(model => model.IsActive).HtmlAttributes(new { id = "organisation_active" } )
(2):@Html.CheckBoxFor(model => model.IsActive).HtmlAttributes(new { id = "contact_active" } )
关于asp.net-mvc - MVC 5 Html helper CheckBoxFor无法在Razor部分中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25986527/