从json返回时只显示值而不显示数据

时间:2017-07-27 09:39:41

标签: jquery json ajax asp.net-mvc login

登录用户,我创建一个模态,当用户点击登录时,如果用户名或密码不正确,请返回 SignInStatus.Failure ,好的, 但页面刷新了,我的页面顶部只有“3”,没有任何想法

但我希望在登录模式表格ex = "user name or password is not correct"

之上显示消息

enter image description here

我的代码

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> _LoginPopup(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return PartialView("ModalPopUp");
    }

    // This doesn't count login failures towards account lockout
    // To enable password failures to trigger account lockout, change to shouldLockout: true
    var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
    switch (result)
    {
        case SignInStatus.Success:
            MigrateShoppingCart(model.Email);
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return Json(SignInStatus.Failure, JsonRequestBehavior.AllowGet);
    }
}

我的模型局部视图是

 @using (Html.BeginForm("_LoginPopup", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
        {
                @Html.AntiForgeryToken()
            <div id="failorLogin"></div>
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    <div class="col-md-12">
                        @Html.TextBoxFor(m => m.Email, new { @class = "form-control", placeholder = "Email" })
                        @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-12">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control", placeholder = "password" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(model => model.RememberMe, htmlAttributes: new { @class = "control-label col-md-8" })
                    <div class="col-md-4">
                        <div class="checkbox">
                            @Html.EditorFor(model => model.RememberMe)
                            @Html.ValidationMessageFor(model => model.RememberMe, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="pull-right col-md-3">
                        <input type="submit" id="btnLogin" value="Login" class="btn btn-default" style="background-color:#5d1338;color:white" />
                    </div>
                </div>

            }

和ajax代码

$("#btnLogin").click(function (e) {
    e.defaultPrevented();
    $.ajax({
        url: '@Url.Action("../Account/_LoginPopup")',
        contentType: "application/json; charset=utf-8",
        dataType: 'json',
        type: "POST",
        data: {
            model:model,
            "__RequestVerificationToken":
                $("input[name=__RequestVerificationToken]").val() 
        },
        success: function(data) {

            alert(data);

        }
    }); 
}); 

1 个答案:

答案 0 :(得分:0)

问题是因为虽然您正在发出AJAX请求,但仍会发生library(huxtable) dat <- sample_n(as.data.frame(bind_cols(mtcars, mtcars, mtcars, mtcars)), 5) ht <- as_hux(dat, add_colnames = TRUE) # now format to taste: bold(ht)[1,] <- TRUE ht[,1:5] # first 5 columns. Will print as LaTeX within a Rmarkdown document 元素提交。这反过来取消了AJAX请求(当页面被卸载时),然后重定向到部分视图。这就是您在页面中看到单独<form>的原因。

要解决此问题,请将您的活动挂钩到3元素的submit并致电<form>,如下所示:

preventDefault()

请注意,我在此处使用$("form").submit(function(e) { e.preventDefault(); $.ajax({ url: this.action, type: this.method, contentType: "application/json; charset=utf-8", dataType: 'json', data: { model: model, "__RequestVerificationToken": $("input[name=__RequestVerificationToken]").val() }, success: function(data) { alert(data); } }); }); 作为选择器,但我建议您在生产代码中使用更具体的内容。例如,form选择器。

另请注意,您已指定id,但您的操作中的大多数逻辑结果都会产生HTML响应。