Ajax.BeginForm - 显示验证错误

发布于 2024-08-08 07:40:49 字数 541 浏览 3 评论 0原文

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

  1. 这是如何指定 Register.aspx 表单的。

    <% using (Html.BeginForm()) { %>;
    
  2. 选择“注册”按钮而不提供任何帐户信息会显示这一点。帐户创建失败。请更正错误并重试。

    • 您必须指定用户名。
    • 您必须指定电子邮件地址。
    • 您必须指定 6 个或更多字符的密码。

  3. 我将 Register.aspx 表单更改为此。

    <% using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post" })) { %>;
    
  4. 选择“注册”按钮而不提供帐户信息不会显示任何错误。

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

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

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

    <% using (Html.BeginForm()) { %>
    
  2. Selecting the Register button without supplying any account info shows this. Account creation was unsuccessful. Please correct the errors and try again.

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

  3. I changed the Register.aspx form to this.

    <% using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post" })) { %>
    
  4. Selecting the Register button without supplying an account info shows no errors.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2024-08-15 07:40:49

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

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 视图中:

<div id="validationSummary">
    <% Html.RenderPartial("ValidationSummary"); %>
</div>

<% using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "validationSummary" })) { %>
    FORM CODE HERE
<% } %>

最后修改 Register 操作:

[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 内。


更新:

最后一个问题 - 而不是
错误消息的项目符号列表,怎么办
我收到每个控件的错误消息
使用以下命令在控件旁边渲染
Html.ValidationMessage("....") 方法

为了实现这一点,您需要将表单的内容放入部分视图中:

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

并在您的注册操作中呈现正确的部分:

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

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:

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

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
<% } %>

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();
    }
}

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.


UPDATE:

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文