-1
我正在使用[Required]
进行字段验证。情景是,有两个复选框,并且基于一个选择,用户必须在textbox
中添加值。如果它是空的,则显示验证错误。问题是,它第一次验证它,但在之后的复选框选择上显示验证消息。我在这里错过了什么?模型验证的数据注释在mvc4中没有按要求工作
型号:
[DisplayName("Flat Fee Amount")]
[Required(ErrorMessage = "Enter flat fee amount.")]
public string FlatFeeAmount { get; set; }
检视:
<div id="show-delivery-fee">
<div>
<%= Html.RadioButtonFor(m => m.DeliveryFee, "All", new {id = "All"}) %>
<%= Html.Label("All", new {@for = "All"}) %>
</div>
<div>
<%-- BASED ON THIS CHECKBOX SELECTION DISPLAY VALIDATION MESSAGE--%>
<%= Html.RadioButtonFor(m => m.DeliveryFee, "FlatFee", new {id = "FlatFee"}) %>
<%= Html.Label("Flat Fee", new {@for = "FlatFee"}) %>
</div>
<div>
<%= Html.LabelFor(m => m.FlatFeeAmount) %>
<%= Html.TextBoxFor(m => m.FlatFeeAmount, new {maxlength = "5"}) %>
<%= Html.ValidationMessageFor(m => m.FlatFeeAmount)%>
</div>
</div>
控制器:
[HttpPost]
[RequiredAccessRights(AllowRightsOr = new[] { SystemRight.EditDelivery })]
[AuditActionFilter("Save store delivery. Store id: {model.StoreId.Value}")]
public ActionResult Delivery(StoreDeliveryModel model)
{
if(this.ModelState.IsValid)
{
try
{
this.StoreManager.SaveDeliveryModel(model);
model.Submitted = true;
}
catch (ValidationException exc)
{
this.ModelState.AddModelError("", exc.Message);
}
}
return View(model);
}
输出:
你想要一个条件验证属性 - 例如,[万无一失(http://foolproof.codeplex.com/)'[RequiredIf]'或同级(或写自己) –
你是对的。我会发布我的答案。 – CSharper