【发布时间】:2012-01-07 08:52:52
【问题描述】:
在我的网站上,我想在所有页面上显示一个登录框。所以我想制作一个局部视图页面,我在 Shared 中的 _Layout.cshtml 文件中使用它。
但是这个局部视图的控制器会去哪里呢?我的登录按钮如何访问它?
所以,当 Request.IsAuthenticated 为真时,登录框会显示“登录为 ...”,但当结果为假时,我会得到一个带有通常用户名/密码表单的小表格。
编辑:在尝试了下面的一些答案之后,我似乎在下面的 GET 方法上陷入了无休止的问题。是因为我的局部视图试图加载我的 _Layout.cshtml 文件,因为它想将“母版页”与局部视图相结合?而且因为我的部分视图是在 _Layout.cshtml 中呈现的,所以它会停止吗?
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel loginModel)
{
if(ModelState.IsValid)
{
var g = new GallaryImage();
var user = g.LoginUser(loginModel.Username, loginModel.Password);
if(user != null)
{
FormsAuthentication.SetAuthCookie(user.username, false);
return RedirectToAction("index", "Home");
}
ModelState.AddModelError("", "Invalid Username/Password");
}
return View();
}
在我的 _Layout.cshtml 中,我尝试像这样加载局部视图:
<div style="text-align: right">
@Html.Action("Index", "Login")
</div>
看到问题了吗?
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-3 partial-views login-control