ASP.Net MVC 3的验证上当作ajaxFormNet、ASP、ajaxForm、MVC

2023-09-10 16:59:48 作者:人心总是伤感的、

我有剃刀视图引擎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]
  [显示(名称=电子邮件)]
  公共字符串电子邮件{获得;组; }
 
ASP NET MVC 2

解决方案

更新

好吧,显然你使用 Ajax.BeginForm ,它使用MicrosoftAjax jQuery中代替(我没有意识到之前)。这其中需要一些额外的工作,以使客户端验证:

您需要

 <%Html.EnableClientValidation(); %>
 

放在你的网页,并且还链接 MicrosoftAjax.js MicrosoftMvcAjax.js MicrosoftMvcValidation.js

下面是这可能是有趣的链接给你:

  

ASP.NET MVC客户端验证通过Ajax.BeginForm

要启用客户端验证定义验证属​​性(我猜 [RequiredWithMessage] 是其中之一),你必须实现的 IClientValidatable接口。

下面是解释如何做到这一点的一篇文章:

  

完全指南验证在ASP.NET MVC 3 - 第2部分

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:

ASP.NET MVC Client Side Validation With Ajax.BeginForm

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:

The Complete Guide To Validation In ASP.NET MVC 3 - Part 2