我在MVC 3架构中设置一些嵌套视图时遇到了麻烦。这就是我目前所拥有的:
_Layout.cshtml
Logon.cshtml
Login.cshtml
ForgotPassword.cshtml
基本上,我的_Layout.cshtml页面只是我的母版页面。然后我有Logon.cshtml。这是我登录页面的“主页”。在那之下,我有两个部分视图,Login.cshtml和ForgotPassword.cshtml。在所有这些背后,我有一个名为AccountController的控制器。首先,让我向您展示Logon.cshtml
查看/帐户/ Logon.cshtml
@model Coltrane.Web.Website.Models.Account.LogonViewModel
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Logon";
}
@*@this.CssIncludes(@Url.Content("~/Content/login.css"))*@
@this.ScriptIncludes(@Url.Content("~/Content/scripts/Account/logon.js"))
@this.InitFunctionIncludes("lga_loginTabs.init()")
@this.InitFunctionIncludes("lga_formFocus.init()")
<div id="container">
<div class="sectionFocus noBg">
<div style="margin: 24px auto; width: 450px;">
<div class="customerLogo" style="margin: 24px auto;">
logo
</div>
<div id="tabContainer" style="position: relative; width:100%;">
<div id="login" class="box_c" style="position:absolute; width:100%;">
<h1>User Login</h1>
<div class="box_c_content">
<div class="sepH_c">
@{ Html.RenderAction("Login", "Account"); }
<a href="#" id="gotoFogot">Forgot your password?</a>
</div>
</div>
</div>
<div id="forgot" class="box_c" style="position: absolute; width:100%;display: none;">
<h1>Forgot Your Password?</h1>
<div class="box_c_content">
<div class="sepH_c">
@{ Html.RenderAction("ForgotPassword", "Account"); }
<a href="#" id="gotoLogin">Back to Login</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
请注意,我已使用Html.RenderAction呈现Login和ForgotPassword部分视图。我这样做是因为其中每个都是形式,他们需要发布到AccountController。接下来,让我向您展示我的Login.cshtml文件:
查看/帐户/ Login.cshtml
@model Coltrane.Web.Website.Models.Account.LoginViewModel
@{
Layout = null;
}
@this.ScriptIncludes(@Url.Content("~/Content/scripts/Account/login.js"))
@this.InitFunctionIncludes("login.init()")
<div class="sepH_a">
<label>
Please enter your email and password to log into the system.</label>
</div>
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "form_login" }))
{
<div class="sepH_a">
<div class="loginError">
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@Html.ValidationMessageFor(m => m.UserName)<br />
@Html.ValidationMessageFor(m => m.Password)
</div>
</div>
<div class="sepH_a">
@Html.LabelFor(m => m.UserName, new { @class = "lbl_a" })
<br />
@Html.TextBoxFor(m => m.UserName, new { @class = "inpt_a_login", id = "lusername", name = "lusername" })
<span class="inputReqd" style="visibility: visible;">*</span>
</div>
<div class="sepH_a">
@Html.LabelFor(m => m.Password, new { @class = "lbl_a" })
<br />
@Html.PasswordFor(m => m.Password, new { @class = "inpt_a_login", id = "lpassword", name = "lpassword" })
<span class="inputReqd" style="visibility: visible;">*</span>
</div>
<div class="sepH_a">
@Html.CheckBoxFor(m => m.RememberMe, new { @class = "input_c", id = "remember" })
@Html.LabelFor(m => m.RememberMe, new { @class = "lbl_c" })
</div>
<div class="box_c_footer">
<button class="sectB btn_a" type="submit">
Login</button>
</div>
}
如您所见,这只是一个简单的表格。需要注意的一点是,我试图在这里使用两种模型。每个观点一个。一个用于父级,一个用于两个子视图(forgotpassword也有一个)。无论如何,让我告诉你我的AccountController
控制器/ AccountController.cs
public class AccountController : Controller
{
private IAuthenticationService _authenticationService;
private ILoggingService _loggingService;
public AccountController(IAuthenticationService authenticationService,
ILoggingService loggingService)
{
_authenticationService = authenticationService;
_loggingService = loggingService;
}
public ActionResult Logon()
{
return View();
}
[HttpGet]
[ChildActionOnly]
public ActionResult Login()
{
return PartialView();
}
[HttpPost]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
else
{
ModelState.AddModelError("", "An error has occurred.");
}
// If we got this far, something failed, redisplay form
return View("Logon");
// return View(model)
// return RedirectToAction("Index", "Home");
}
//........
}
在方法[HttpPost]登录(...)上,我正在检查用户信用是否正确。如果是这样,一切正常,但问题是出错。如果我使用return View("Logon");
我认为我得到一个无限循环,如果我使用return View(model)
我得到了Login.cshtml的局部视图,如果我使用return RedirectToAction("Index", "Home");
,我会回到我的登录视图正确,但我添加的错误没有显示(他们确实出现在我return View(model);
。我在这里做错了什么?我认为我的设置有意义,但我无法弄清楚为了使它正常工作。我希望返回以呈现带有错误的Logon.cshtml视图。为了完成,这里是我提供的视图的两个ViewModel:
public class LogonViewModel
{
}
public class LoginViewModel
{
[Required]
[Display(Name = "Email")]
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; }
}
答案 0 :(得分:2)
在我看来,处理此问题的最佳方法是重定向回索引页面。但正如你所提到的,这会导致你的错误丢失。
虽然有一个很好的解决方案 - 在TempData中保存模型状态并在Index页面上重新导入它。这是一个链接,它提供了一些很好的属性,您可以使用这些属性使其非常无缝:[ImportModelStateFromTempData]
和[ExportModelStateToTempData]
http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx#prg
答案 1 :(得分:0)
我认为问题在于您正在使用的方法。
@Html.RenderAction("Login", "Account")
将始终调用GET Login,它没有与之关联的数据。
尝试使用:
@Html.RenderPartial("Login","Account", Model.LoginModel)
其中Model是LogonViewModel,LoginModel是LoginViewModel。
您的LogonViewModel将变为:
public class LogonViewModel
{
public LoginViewModel LoginModel { get; set; }
}
然后在您的AccountController中,您可以执行以下操作:
var logonModel = new LogonViewModel();
logonModel.LoginModel = model;
return View("Logon", logonModel);