在部分中使用Ajax.beginform显示验证消息

时间:2016-09-07 00:49:44

标签: ajax asp.net-mvc asp.net-mvc-4 partial-views

我是.NET mvc 5环境中的新手。

  

我有一个观点:

Index.cshtml

@model Accounts.WebHost.Models.SendUsernameReminderInputModel

@using (Ajax.BeginForm("Index", "SendUsernameReminder", null, new AjaxOptions { InsertionMode = InsertionMode.Replace, UpdateTargetId = "validationList", LoadingElementId = "loader", OnSuccess = "onSuccess", OnFailure = "onFailure" }, new { @id = "validationForm", @class = "form-inline" }))
{
    @Html.AntiForgeryToken()

        <div class="form-group">
            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
        </div>

        <div class="form-group hidden">
            @Html.TextBoxFor(model => model.Tenant, new { @class = "form-control", @Value = "default" })
        </div>
        <button type="submit" class="btn btn-default">Submit</button>
}
<hr />

<div id="loader" class="alert" style="display:none">
    <img src="~/Content/img/ajax-loader.gif" />
</div>

@Html.Partial("_UsernameValidation")
  

部分观点:

_UsernameValidation.cshtml

@model Accounts.WebHost.Models.SendUsernameReminderInputModel

<div id="validationList">
    <table>

            <tr>
                <td>@Html.ValidationMessageFor(model => model.Email)</td>
                <td>@Html.ValidationMessageFor(model => model.Tenant)</td>
            </tr>

    </table>
</div>
  

这是我的控制器:

SendUsernameReminderController.cs

using Accounts.Entities.Models;
using Accounts.WebHost.Models;
using BrockAllen.MembershipReboot;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Accounts.WebHost.Controllers
{
    public class SendUsernameReminderController : Controller
    {
        public readonly UserAccountService<MemberAccount> userAccountService;

        public SendUsernameReminderController(UserAccountService<MemberAccount> userAccountService)
        {
            this.userAccountService = userAccountService;
        }

        [HttpGet]
        public ActionResult Index(string signin)
        {
            ViewBag.Signin = signin;

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(SendUsernameReminderInputModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    this.userAccountService.SendUsernameReminder(model.Tenant, model.Email);
                    return RedirectToAction("Success", model);
                }
                catch (ValidationException ex)
                {
                    if (ex.ValidationResult.ToString() == "The email address for this account is not yet verified.")
                    {
                        try
                        {
                            userAccountService.ResetPassword(model.Tenant, model.Email);
                            return RedirectToAction("Unverified");
                        }
                        catch (ValidationException resetex)
                        {
                            ModelState.AddModelError("", resetex.Message);
                            ViewBag.Message = "resetex.Message";
                            return View();
                        }
                    }
                    ModelState.AddModelError("", ex.Message);
                    return View();
                }
            }
            return View();
        }

        public ActionResult Success(SendUsernameReminderInputModel model)
        {
            ViewBag.Subject = "Username Emailed";
            ViewBag.Message = "Your username was emailed at " + model.Email + ". If you don't receive this email within 24 hours, please check your junk mail folder or visit our Help pages to contact Customer Service for further assistance.";
            return View();
        }

        public ActionResult Unverified()
        {
            ViewBag.Subject = "Email has not been verified";
            ViewBag.Message = "You will receive an email from us to confirm your email or cancel your registration. If you don't receive this email within 24 hours, please check your junk mail folder or visit our Help pages to contact Customer Service for further assistance.";
            return View();
        }
    }
}
  

这是我的模特:

SendUsernameReminderInputModel.cs

using System.ComponentModel.DataAnnotations;

namespace Accounts.WebHost.Models
{
    public class SendUsernameReminderInputModel
    {
        [Required]
        [EmailAddress]
        public string Email
        {
            get;
            set;
        }

        [Required]
        public string Tenant
        {
            get;
            set;
        }
    }
}

我的目标是,当用户点击表单提交按钮 时,验证消息将显示在表单下方。不幸的是,它会在部分输出整个Index.cshtml,在底部输出验证消息。

如果这是一个不好的方法,请给我指示。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

你需要像Model那样通过View(model);以便View获取具有这些错误的模型。 你还可以使用@Html.ValidationSummary来显示所有错误