2011-11-04 71 views
6

我想构建一个登录表单,该表单显示在我网站中每个页面的侧边栏中。如果用户输入了不正确的用户/密码,我希望在此表单上方显示错误(其余页面保持原样),如果他成功登录,我希望表单更改为列表关于用户的信息(同样,页面的其余部分与登录前相同)。我正在使用默认的Internet应用程序模板的MVC 3 Web应用程序项目。我有这样的:用户在部分视图中登录,显示错误

_Layout.cshtml

@{ 
    if (User.Identity.IsAuthenticated) 
    { 
     Html.RenderAction("ShowUserInfo", "User"); 
    } 
    else 
    { 
     Html.RenderAction("LogIn", "User"); 
    }   
} 

UserController的

[ChildActionOnly] 
    public PartialViewResult ShowUserInfo() 
    { 
     // populate loggedInInfo from database based on 
     // User.Identity.Name 
     return PartialView("_LoggedInInfo", loggedInInfo); 
    } 

    private ActionResult RedirectToPrevious(string returnUrl) 
    { 
     if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
      && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
     { 
      return Redirect(returnUrl); 
     } 
     else 
     { 
      return RedirectToAction("index", ""); 
     } 
    } 

    [ChildActionOnly] 
    public PartialViewResult LogIn() 
    { 
     return PartialView("_LogInForm"); 
    } 

    // 
    // POST: /User/LogIn 

    [HttpPost] 
    public ActionResult LogIn(LogInModel model, string returnUrl) 
    { 
     if (ModelState.IsValid) 
     { 
      if (Membership.ValidateUser(model.UserName, model.Password)) 
      { 
       FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); 

       return RedirectToPrevious(returnUrl); 
      } 
      else 
      { 
       ModelState.AddModelError("", "The user name or password provided is incorrect."); 
      } 
     } 

     return RedirectToPrevious(returnUrl); 
    } 

_LogInForm

@model MyProject.Models.LogInModel 

<h2>Login</h2> 
<p> 
    Please enter your username and password. @Html.ActionLink("Register", "register", "user") if you don't have an account.<br /> 
    @Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.") 
</p> 

@using (Html.BeginForm("LogIn", "user")) { 
    html stuff 
} 

除了当我输入错误的用户名/密码时,页面只是用一个空的表单重新加载,并且不显示错误,它几乎按照预期工作。我也尝试过其他一些东西,但是我要么得到错误信息,不能从部分视图发出重定向,要么显示部分视图(显示错误)作为整体视图显示,因此它显示为单个视图页面,与网站的其他部分分开。如果我登录正确,一切正常。

如何才能在窗体上方正确显示错误?我宁愿不使用任何Ajax或JQuery来做到这一点。

回答

3

问题似乎是你正在做一个重定向,而不是仅仅返回appropriet视图。

您已经添加了一个模型的错误后,你需要自行返回执行重定向的观点:

return View("LoginViewNameGoesHere")

所以你不想回到这里的局部视图,但整个视图。

+0

也许我错过了一些非常明显的东西,但我不明白。我不知道我的观点是什么 - 它可以是任何东西。与登录过程有关的部分视图在每个页面中呈现。那么我会返回哪个视图?我没有处理登录信息的专门视图,只是我提到的部分视图。 – IVlad

+0

好吧,你的部分登录视图可以显示在任何视图页上?然后,我能想到的唯一选择是使用Ajax并使用“更新目标”,然后您可以返回部分登录视图,并且部分登录视图的占位符将相应更新。你知道我要去的地方吗?因为在这种情况下你不能使用重定向。 –

+0

是的,我的部分登录视图显示在任何地方。你能解释一下如何使用Ajax或为此推荐一个教程吗? – IVlad

0

做时,RedirectToActionRedirect,当前请求与http status code for redirection - 3xx结束,它告诉浏览器进行另一个请求到指定的URL。这意味着,当前请求的所有验证数据都会丢失,并且全新的纯请求用于登录url。你会得到空的形式,没有错误。

你应该做的是在当前请求范围内呈现登录视图,而不是通过重定向。重新显示无效视图的一般模式

public ActionResult Login(LogInModel model) 
{ 
    if(ModelState.IsValid) 
    { 
     return RedirectToAction("Home"); 
    } 
    return View(model); //! no redirection 
} 
+0

这就是我在帖子中告诉他的。 –