簡體   English   中英

布局的動作結果

[英]ActionResult for Layout

我正在嘗試制作一個位於Layout本身而不是任何View中的登錄表單。 問題是我不知道如何為布局編寫ActionResult。

如果我把代碼放進去,比如說ActionResult Index(),那么它將僅在索引頁面上起作用。 那么Layout本身是否有類似ActionResult的東西?

我會這樣做:

  1. 創建一個登錄操作以返回特定的部分。

     public ActionResult Login() { if (User.Identity.IsAuthenticated) { return PartialView("_loggedInPartial"); } else { return PartialView("_notLoggedInPartial"); } } 
  2. 在您的layout.cshtml中這樣調用它:

     @Url.Action("Login", "Account"); 

更新資料

您還可以檢索用戶並將其返回到_notLoggedInPartial視圖,以顯示一些用戶憑據或如下所示的歡迎消息:

  ...
  else
  {
    // User retrieval code from db
    return PartialView("_notLoggedInPartial", model);
  }
  ...

人們通常不喜歡這樣做,但仍然是實現這一目標的最簡單方法

  1. 在具有登錄字段的布局本身中創建一個表單(html / ajax)。
  2. 從那里發布到您的“登錄發布”操作。

     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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM