我有一个ASP.Net MVC application
,这就是我的ViewModel的样子。
public class Customer
{
[Required(ErrorMessage ="Name is required")]
public string Name { get; set; }
//using Expressive Annotations
[RequiredIf("Name == 'Xyz'")]
public string Email { get; set; }
}
我的Index.cshtml看起来像
@model ea.sample.Models.Customer
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Customer</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts{
@Scripts.Render("~/bundles/jquery");
@Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/expressive-annotations-validate/2.7.4/expressive.annotations.validate.min.js"></script>
}
但是当我提交表单时,如果Name为空,则触发ClientSide验证,但对于电子邮件,不会触发条件验证。
请提出建议。谢谢!