MVC 客户端验证
Posted
技术标签:
【中文标题】MVC 客户端验证【英文标题】:MVC Client Side Validation 【发布时间】:2012-07-29 01:58:11 【问题描述】:学习 MVC 3,我正在尝试使用 AJAX 在 MVC 中使用客户端验证:
我在文章为 ajax 帖子手动调用 MVC 3 客户端验证中包含了建议,但这对我不起作用,它仍然认为表单是有效的。我错过了什么??
我的申请中包含以下内容:
根 web.config 文件:
<appSettings>
<add key="webpages:Version" value="1.0.0.0" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusivejavascriptEnabled" value="true" />
</appSettings>
布局页面脚本:
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
我的模特:
[Table("Person")]
public class Person
[Key]
public int PersonID get; set;
[Column("FullName")]
[Required(ErrorMessage = "Give me a name.")]
public string NameFull get; set;
[Required(ErrorMessage = "Give me an email.")]
public string EmailAddress get; set;
[Required(ErrorMessage = "Give me a mobile number.")]
public string MobileNo get; set;
[ForeignKey("Agency")]
[Required(ErrorMessage = "Give me an agency.")]
public int AgencyID get; set;
public virtual Agency Agency get; set;
进行 ajax 调用的方法(触发 onclick 事件):
function LoadPage(SFORM, SACTION, SMETHOD)
$('#divLoading').slideDown(1);
var doc = document.getElementById(SFORM)
var dataform = $(doc).serialize();
var $form = $(doc);
if ($form.valid())
//Ajax call here
$.ajax(
type: SMETHOD,
url: SACTION,
data: dataform,
complete: function ()
$("#divLoading").slideUp(1, function () FinishLoad() );
,
success: function (data)
$("#divMain").html(data)
);
视图 (.cshtml):
<form id="frmCreate" name="frmCreate">
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.NameFull)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NameFull)
@Html.ValidationMessageFor(model => model.NameFull)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.EmailAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.EmailAddress)
@Html.ValidationMessageFor(model => model.EmailAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MobileNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MobileNo)
@Html.ValidationMessageFor(model => model.MobileNo)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AgencyID, "Agency")
</div>
<div class="editor-field">
@Html.DropDownList("AgencyID", String.Empty)
@Html.ValidationMessageFor(model => model.AgencyID)
</div>
<p>
<input type="button" value="Create" onclick="LoadMenuItem('frmCreate','@Url.Action("Create", "Person")', 'POST')" />
</p>
</fieldset>
</form>
【问题讨论】:
您为此关注的任何文章或参考资料? 【参考方案1】:似乎已经想通了,看来您必须使用@using (Html.BeginForm(null, null, FormMethod.Get, new name = "validator", id = "validator" ))
创建表单而不是标准标签,然后使用视图底部的脚本分配要验证的表单喜欢验证$.validator.unobtrusive.parse("#validator");
这样做的好处是,在您的布局页面上,您可能有一个具有不同 ID 的“父表单”,其中可能包含隐藏字段,您可能希望使用经过验证的表单发布这些隐藏字段,您所要做的就是发布父表单,但验证仍将发生在您在部分视图上的脚本$.validator.unobtrusive.parse("#validator");
中分配的表单上。
【讨论】:
以上是关于MVC 客户端验证的主要内容,如果未能解决你的问题,请参考以下文章