我正在尝试在客户端验证我的模型(在回发之前),但它似乎不起作用。这是我的一段代码:
看法:
@section scripts{
<script type="text/javascript" src="@Url.Content( "~/scripts/jquery.validate.js")"></script>
<script type="text/javascript" src="@Url.Content( "~/scripts/jquery.validate.unobtrusive.js")"></script>
<script type="text/javascript" src="@Url.Content( "~/scripts/jquery.unobtrusive-ajax.js")"></script>
}
@using(Ajax.BeginForm("AddPhase", new AjaxOptions {
UpdateTargetId = "newPhaseSeparator",
InsertionMode = InsertionMode.InsertAfter
})) {
@Html.ValidationSummary()
<div id="newPhase">
<input type="text" id="phaseName" name="name" />
<button id="btnAddPhase">Ok</button>
</div>
}
模型:
[Required(ErrorMessage = "Please enter a name")]
public string Name { get; set; }
在AddPhase
操作中,ModelState.IsValid 为 false,这意味着注释确实有效。有没有办法从客户端执行模型验证,从而避免回发?