简体   繁体   English

部分视图和ajax

[英]Partial View and ajax

I want to update Partial View via ajax, but it does not work. 我想通过ajax更新部分视图,但是它不起作用。 Look at this model class: 看这个模型类:

   public class LogOnModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Display(Name = "Remember me?")]
        public bool RememberMe { get; set; }

        public bool IsLoggedIn { get; set; }

        public string ReturnUrl { get; set; }
    }

the following view: 以下视图:

@model ITW2012Mobile.ViewModels.LogOnModel
<div id='LogOn' style="background-color: White;">
    @using (Ajax.BeginForm("LogOnAjax", "Home", new AjaxOptions { UpdateTargetId = "LogOn", OnSuccess = "logInComplete" }))
    { 
        ITW2012Mobile.ViewModels.LogOnModel m = Model;
        @Html.EditorFor(model => model.IsLoggedIn)
        @Html.EditorFor(model => model.ReturnUrl)
        <div>
            @Html.ValidationSummary()
        </div>
        <div>
            @Html.LabelFor(model => model.UserName)
            @Html.EditorFor(model => model.UserName)
        </div>
        <div>
            @Html.LabelFor(model => model.Password)
            @Html.EditorFor(model => model.Password)
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
    }
</div>

and the following controller class: 和以下控制器类:

public ActionResult LogOnAjax(LogOnModel model)
{
    if (!User.Identity.IsAuthenticated)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                model.IsLoggedIn = true;
                model.ReturnUrl = Url.Action("Index", "Home"); 
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return PartialView("PartialViewAjaxLogOn", model);
    }
    else
    {
        return PartialView("PartialViewLogOut");
    }
}

even when username/password are correct and IsLoggedIn = true and ReturnUrl!=empty view shows empty fields for these variables (but debugger shows values inside). 即使用户名/密码正确且IsLoggedIn = true和ReturnUrl!= empty视图也显示这些变量的空字段(但调试器在其中显示值)。 Why and how to make it correctly? 为什么以及如何正确制作?

Try clearing the values you are modifying in your action from modelstate or if you use them in html helpers the old values will be used: 尝试从modelstate清除要在操作中修改的值,或者如果在html helper中使用它们,则将使用旧值:

ModelState.Remove("IsLoggedIn");
model.IsLoggedIn = true;
ModelState.Remove("ReturnUrl");
model.ReturnUrl = Url.Action("Index", "Home"); 

Also bear in mind that upon successful authentication and cookie emission you should not display a view (partial in your case). 还请记住,成功进行身份验证和Cookie释放后,您不应显示视图(部分情况)。 You should redirect so that the authentication cookie is sent by the client on the subsequent request. 您应该重定向,以便身份验证cookie由客户端在后续请求中发送。 You should redirect to the return url. 您应该重定向到返回网址。 But since you are doing this using AJAX you should probably send some indication to the client that the authentication was successful so that you can redirect on the client. 但是,由于您是使用AJAX进行此操作的,因此您可能应该向客户端发送一些认证成功的指示,以便您可以在客户端上重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM