我正在进行MVC 4练习项目,其中我有多个部分视图,当我提交其中一个时,整个页面都会验证。我希望每个按钮只验证自己的形式。
页面如下所示:
这是一个局部视图示例:
@model Money.Models.Category
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@using (Html.BeginForm("Create", "Category", FormMethod.Post, new { id = "createCategoryForm" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Category</legend>
<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>
<p>
<input id="createCategorySubmit" type="submit" value="Create" />
</p>
</fieldset>
}
这是整个观点的一部分:
@model Money.Models.Transaction
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Transaction</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CategoryId, "Category")
</div>
<div class="editor-field">
@Html.DropDownList("CategoryId", String.Empty)
@Html.ValidationMessageFor(model => model.CategoryId)
</div>
<div id="createCategory">
@Html.Partial("~/Views/Shared/_CreateCategory.cshtml")
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AccountId, "Accout")
</div>
<div class="editor-field">
@Html.DropDownList("AccountId", String.Empty)
@Html.ValidationMessageFor(model => model.AccountId)
</div>
<div id="createAccount">
@Html.Partial("~/Views/Shared/_CreateAccount.cshtml")
</div>
你有什么想法吗?
谢谢
答案 0 :(得分:0)
您必须使用验证器解析新内容:
$('form').data('validator', null);
$.validator.unobtrusive.parse($('form'));