【问题标题】:MVC 4: Return partial view inside of bootstrap modal upon failed validationMVC 4:在验证失败时返回引导模式内的部分视图
【发布时间】:2014-11-01 14:26:13
【问题描述】:

我正在使用 MVC 4 和 SimpleMembership 用于帐户处理。当用户登录网页时,我正在使用 Boostrap V3.2.0 模式。模式工作正常,甚至通过“Ajax.BeginForm”处理模型状态验证我遇到的问题是验证模型状态后,如果登录失败(用户输入的用户名或密码不正确),帐户控制器的登录操作结果返回登录页面的 PartialView。它不是在 Modal 中加载部分视图,而是作为整页加载(仍然是部分视图,没有 _Layout 页面)。登录失败时,如何使部分视图加载回登录模式?

这是我的模式登录视图中的代码:

@model AdorationSuite.Models.LoginModel

<script type="text/javascript" src="/scripts/jquery.validate.min.js"></script>
<script type="text/javascript" src="/scripts/jquery.validate.unobtrusive.min.js"></script>

@{AjaxOptions options = new AjaxOptions();
options.HttpMethod = "POST";
options.Url = Url.Action("Login", "Account");
options.UpdateTargetId = "modal-body";
options.InsertionMode = InsertionMode.Replace;
}

@using (Ajax.BeginForm("Login", "Account", options, new {id="edit-form"})) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false, "Come on now, get it together man!!")

    <div class="modal-header" style="height:auto">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="ModalLabel">Log In</h4>
    </div>

    Html.RenderPartial("~/Views/Account/Partials/LoginForm.cshtml", Model);

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="submit" id="submit-login" value="Login" class="btn btn-primary">Login</button>
    </div>
}

这是我控制器中的代码:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.EmailAddress, hashPassword(model.Password), persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The email address or password provided is incorrect.");

            return PartialView("Login", model);
        }

当发生模型状态错误时,例如,如果用户没有输入用户名或密码,则表单验证工作正常,并且模式更新时验证错误。但是如果Modelstate验证通过,但是Login失败,当控制器返回如下:

return PartialView("Login", model);

该部分视图作为一个完整页面加载(在 _Layout 页面之外),而不是在登录模式中。

关于为什么在登录失败时没有将结果发布回登录模式,我有什么明显的遗漏吗?

提前致谢。

【问题讨论】:

    标签: ajax asp.net-mvc-4 twitter-bootstrap-3 bootstrap-modal


    【解决方案1】:

    我认为您将需要一些客户端代码。

    试试:

    <div id="partialSummaryDiv"> Html.RenderPartial("~/Views/Account/Partials/LoginForm.cshtml", Model);</div>
    

    事件你将有你的 ajax 调用

    $.ajax({
    url: "/Controller/Action",
    type: "POST",
    success: function (result) {
         // refreshes partial view
        $('#partialSummaryDiv').html(result);
    }
    });
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多