我有一个奇怪的问题,即部分视图(由Ajax.ActionLink呈现)中不显眼的验证不起作用。
这是部分视图:
@model MyWeb.Models.PersonSkill
@using (Ajax.BeginForm("EditSkill", null, new AjaxOptions { UpdateTargetId = "skills" }, new { id = "EditSkillForm" }))
{
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.YearsOfExperience)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.YearsOfExperience)
@Html.ValidationMessageFor(model => model.YearsOfExperience)
</div>
<p>
<input type="submit" value="Save"/>
</p>
}
这被称为:
@Ajax.ActionLink("Edit", "LoadEditSkill", new { id = item.Id }, new AjaxOptions() { UpdateTargetId = "editSkillDialog" }, new { @class = "editSkill" })
视图很好。它将数据发回服务器,但它不会验证。
答案 0 :(得分:5)
非常感谢 Joe Tuskan 指出我正确的方向。
我通过在我的通话中添加一个OnSuccess订阅者来修复此问题:
@Ajax.ActionLink("Edit", "LoadEditSkill", new { id = item.Id }, new AjaxOptions() { UpdateTargetId = "editSkillDialog", OnSuccess = "onSuccess" }, new { @class = "editSkill" })
并添加调用解析(),如解释here:
var onSuccess = function () {
// enable unobtrusive validation for the contents
// that was injected into the <div id="result"></div> node
$.validator.unobtrusive.parse($("#editSkillDialog"));
};
答案 1 :(得分:1)
始终确保包含对
的必要参考jquery.validate.min.js和jquery.validate.unobtrusive.min.js,当然还有主要的jquery库,这通常是最常见的问题,正如我在cshtml中看到的那样,它们不存在于你的页面要么。