ASP.Net MVC 3的验证上当作ajaxForm [英] ASP.Net MVC 3 validation on AjaxForm
问题描述
我有剃刀视图引擎ajax的形式。为了验证我使用dataanotation类。当用户提交表单验证做工精细,验证消息做工精细。 问题是,确认不会工作KEYUP或模糊的事件。
我怎样才能激活验证没有提交当作ajaxForm(ajax.beginform)
下面是我的看法code:
@using(Ajax.BeginForm(新AjaxOptions {InsertionMode = InsertionMode.Replace,
UpdateTargetId =employeeDetail,HttpMethod =邮报的onComplete =完成,
确认=确认? }))
{
@ Html.TextBoxFor(型号=> model.Email)
@ Html.ValidationMessageFor(型号=> model.Email)
<跨度风格=浮动:权利><输入类型=提交级=tableGenelButtonID =提交按钮值=Kaydet/>< / SPAN>
}
型号:
[RequiredWithMessage]
[显示(名称=电子邮件)]
公共字符串电子邮件{获得;组; }
更新
好吧,显然你使用 Ajax.BeginForm
,它使用MicrosoftAjax jQuery中代替(我没有意识到之前)。这其中需要一些额外的工作,以使客户端验证:
您需要
<%Html.EnableClientValidation(); %>
放在你的网页,并且还链接 MicrosoftAjax.js
, MicrosoftMvcAjax.js
和 MicrosoftMvcValidation.js
下面是这可能是有趣的链接给你:
<一个href="http://weblogs.asp.net/imranbaloch/archive/2010/07/18/asp-net-mvc-client-side-validation-with-ajax-beginform.aspx"相对=nofollow> ASP.NET MVC客户端验证通过Ajax.BeginForm
要启用客户端验证定义验证属性(我猜 [RequiredWithMessage]
是其中之一),你必须实现的 IClientValidatable接口。
下面是解释如何做到这一点的一篇文章:
I have an ajax form on razor view engine. For validation i use dataanotation classes. Validation work fine when user submit the form, validation messages work fine. The problem is, validation wont work on keyup or blur events.
How can i activate validation without submit on ajaxform(ajax.beginform)
Here is my view code:
@using (Ajax.BeginForm(new AjaxOptions { InsertionMode = InsertionMode.Replace,
UpdateTargetId = "employeeDetail", HttpMethod = "Post", OnComplete = "Complete",
Confirm = "Confirm?" }))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model=>model.Email)
<span style="float:right"><input type="submit" class="tableGenelButton" id="submitButton" value="Kaydet" /></span>
}
Model:
[RequiredWithMessage]
[Display(Name = "E-Mail")]
public string Email { get; set; }
Update:
Ok, apparently you're using Ajax.BeginForm
which uses MicrosoftAjax in stead of jQuery (I didn't realize that before). This one needs some extra work to enable client side validation:
You need
<% Html.EnableClientValidation(); %>
Somewhere in your page, and also links to MicrosoftAjax.js
, MicrosoftMvcAjax.js
and MicrosoftMvcValidation.js
Here's a link that might be interesting for you:
To enable client side validation for a custom validation attribute (I guess [RequiredWithMessage]
is one of those), you have to implement the IClientValidatable interface.
Here is an article that explains how to do that:
这篇关于ASP.Net MVC 3的验证上当作ajaxForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!