我使用ASP.NET MVC 5,并且具有以下代码:
布局:
<script src="@Url.Content("~/Scripts/jquery-2.0.3.js")" type="text/javascript"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/Main.js")"></script>
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@Scripts.Render("~/Scripts/jquery.validate.min.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.min.js")
模型:
public class PatientApplicationViewModel
{
[Display(Name = "Home Phone Number")]
[Phone(ErrorMessage = "Invalid home phone")]
public string HomePhone { get; set; }
并查看:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.HomePhone, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.HomePhone)
@Html.ValidationMessageFor(model => model.HomePhone)
</div>
</div>
不起作用!仅在服务器端Model.IsValid = false。为什么客户端不起作用以及如何修复?
答案 0 :(得分:0)
我认为您的viewmodel HomePhone缺少[必需]标记
应该是:
public class PatientApplicationViewModel
{
[Required]
[Display(Name = "Home Phone Number")]
[Phone(ErrorMessage = "Invalid home phone")]
public string HomePhone { get; set; }