我想做一個登錄窗體,位於佈局本身爲此不在任何視圖。問題是我不知道如何爲佈局編寫ActionResult。佈局的ActionResult
如果我把我的代碼放在讓我們說的ActionResult指數(),那麼它只會在索引頁上工作。那麼有沒有像佈局本身的ActionResult?
我想做一個登錄窗體,位於佈局本身爲此不在任何視圖。問題是我不知道如何爲佈局編寫ActionResult。佈局的ActionResult
如果我把我的代碼放在讓我們說的ActionResult指數(),那麼它只會在索引頁上工作。那麼有沒有像佈局本身的ActionResult?
我會做這樣的:
創建登錄行動回報特定部分。
public ActionResult Login()
{
if (User.Identity.IsAuthenticated)
{
return PartialView("_loggedInPartial");
}
else
{
return PartialView("_notLoggedInPartial");
}
}
叫它您layout.cshtml內是這樣的:
@Url.Action("Login", "Account");
更新
你也可以檢索用戶並返回到_notLoggedInPartial
視圖中顯示一些用戶憑證或類似這樣的歡迎消息:
...
else
{
// User retrieval code from db
return PartialView("_notLoggedInPartial", model);
}
...
人們一般不會喜歡這樣做,但仍低於是實現
從那裏發佈到您的「登錄帖子」操作。
using (Html.BeginForm("Login", "Account")){
@Html.TextBox("Username")
@Html.TextBox("Password")
<input type="submit" value="Login">
}
AccoutController必須包含具有以下結構的動作。
[HttpPost]
Public ActionResult Login(string Username, string Password)
{
//handle appliaction logic
}
否。將您的登錄表單放入通過控制器操作呈現的局部視圖中。然後渲染布局中的局部視圖。 –