Ajax.BeginForm-显示验证错误 [英] Ajax.BeginForm - Displaying Validation Errors

查看:125
本文介绍了Ajax.BeginForm-显示验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VS2008中使用MVC项目模板(开箱即用),我注意到以下内容:

Using the MVC project template in VS2008 (out of the box) I noticed the following:


  1. 这里是

  1. Here is how the Register.aspx form is specified.

<% using (Html.BeginForm()) { %>


  • 选择注册按钮而不提供任何帐户信息将显示此信息。帐户创建失败。请更正错误,然后重试。

  • Selecting the Register button without supplying any account info shows this. Account creation was unsuccessful. Please correct the errors and try again.

    •您必须指定用户名。

    •您必须指定电子邮件地址。

    •您必须指定密码为6个或更多字符。

    • You must specify a username.
    • You must specify an email address.
    • You must specify a password of 6 or more characters.

    我将Register.aspx格式更改为此。

    I changed the Register.aspx form to this.

    <% using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post" })) { %>
    


  • 选择注册按钮而不提供帐户信息不会显示错误。

  • Selecting the Register button without supplying an account info shows no errors.

    问题:使用Ajax.BeginForm时如何显示错误文本?

    Question: How do I get the error text to display when using Ajax.BeginForm ?

    推荐答案

    要成功应用ajax表单提交,您将必须修改Register操作和视图。默认情况下,如果发生错误,此操作仅返回关联的register.aspx视图。第一步是将验证摘要放入部分用户控件中:

    To successfully apply ajax form submission you will have to modify the Register action and the views. By default, in case of error, this action just returns the associated register.aspx view. The first step would be to put the validation summary inside a partial user control:

    ValidationSummary.ascx:

    ValidationSummary.ascx:

    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
    <%= Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.") %>
    

    然后将这个部分包括在Register.aspx视图中:

    Then you include this partial inside the Register.aspx view:

    <div id="validationSummary">
        <% Html.RenderPartial("ValidationSummary"); %>
    </div>
    
    <% using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "validationSummary" })) { %>
        FORM CODE HERE
    <% } %>
    

    最后,您修改了Register动作:

    And finally you modify the Register action:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Register(string userName, string email, string password, string confirmPassword)
    {
        // ... skipped content for clarity
    
        // If we got this far, something failed, redisplay form
        if (Request.IsAjaxRequest())
        {
            // If an ajax request was made return only the validation errors 
            // instead of the whole page
            return PartialView("ValidationSummary");
        }
        else
        {
            return View();
        }
    }
    

    如果默认情况下注册成功,则Register操作只是重定向到主页/索引。您还必须修改此位,因为在执行ajax请求时,重定向将无法进行。您可以通过相同的方式测试是否异步调用操作,并返回一些文本来指示注册成功。此文本将显示在与验证错误相同的 div 内。

    In case the registration succeeds by default the Register action just redirects to Home/Index. You will have to modify this bit as well, because as you are performing an ajax request the redirection won't work. The same way you could test if you are invoking the action asynchronously and return some text that will indicate that the registration was successful. This text will be displayed inside the same div as the validation errors.

    更新:


    最后一个问题-
    如何代替错误消息的
    项目符号列表我使用
    Html.ValidationMessage( ....)方法

    One final question - Instead of the bullet list of error messages, how do I get each controls error message to render next to the control using the Html.ValidationMessage("....") method ?

    要实现此目的,您需要将表单的内容放入局部视图内:

    In order to achieve this you need to put the contents of your form inside a partial view:

    <% using (Ajax.BeginForm(
        "register", 
        null, 
        new AjaxOptions { 
            HttpMethod = "POST", 
            UpdateTargetId = "myForm" 
        }, 
        new { 
            id = "myForm" 
        })) { %>
         <% Html.RenderPartial("RegisterForm"); %>
    <% } %>
    

    然后在您的注册操作中呈现正确的部分内容:

    And in your register action render the correct partial:

    if (Request.IsAjaxRequest())
    {
        return PartialView("RegisterForm");
    }
    else
    {
        return View();
    }
    

    这篇关于Ajax.BeginForm-显示验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆