【发布时间】:2015-05-12 18:45:52
【问题描述】:
我正在尝试验证以下单选按钮是否为真,那么必须至少选择一个复选框。
@using (Html.BeginForm("Print","Home",FormMethod.Post, new{ @class="form", role ="form")
{
@Html.AntiForgeryToken();
<div class="col-md-12">
<div class="alert alert-info">
If you are multiracial, please complete this item by indicating the ethnic/racial group you identify with most or the ethnic/racial group to which you are usually regarded in the community as belonging.
</div>
</div>
<div class="col-md-9">
<div class="form-group">
@Html.LabelFor(model => model.IsMultiracial, "Are you Multiracial?", new { @class = "control-label" })
@Html.RadioButtonFor(model => model.IsMultiracial, "false", new { onclick = "showRacial(this.value)" }) No
@Html.RadioButtonFor(model => model.IsMultiracial, "true", new { onclick = "showRacial(this.value)" }) Yes
</div>
</div>
</div>
<div class="row">
<div id="multiracial" style="display: none;">
<div class="col-md-9">
<div class="form-group">
@for (int i = 0; i < Model.ParentRacialGroups.Count(); i++)
{
<input type="hidden" name="ParentRacialGroups.Index" value="@i" />
<input type="checkbox" id="ParentRacialGroup" name="ParentRacialGroups[@i].ID" value="@Model.ParentRacialGroups[i].ID" checked="@Model.ParentRacialGroups[i].isChecked" data-size="xs" />
<label class="cbx-label small" for="check-2g">@Model.ParentRacialGroups[i].name</label>
<br />
}
</div>
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-offset-2 col-md-4">
<div class="form-group">
<input type="submit" class="btn btn-default" value="Print" />
</div>
</div>
</div>
}
【问题讨论】:
-
只需在 showRacial 方法中传递对象 'this' 并在方法内部检查 if(this.checked == true) then this.value
-
表单将如何验证?
-
你用什么验证?
-
我的 jquery 中已经有了 showRacial,这不是我需要的帮助
-
您所显示的只是视图。在您进行验证的位置显示您的控制器代码。您是否向
ModelState添加了错误?您希望如何以及在何处显示错误?
标签: jquery html asp.net asp.net-mvc validation