2011-09-29 133 views
0

我想顯示有關用戶註冊狀態的信息。它應該顯示在每個視圖上,但我認爲,將該代碼寫入約20次是一個糟糕的主意。順便說一句,這是代碼,它的工作原理,我保證:如何在每個視圖中顯示相似的信息?

查看/ RegistrationInfo:

<table width="95%" border="0" cols="2"> 
    <tr> 
    <td width="50%" height="124"><h2><strong>Simple Blog </strong></h2></td> 
    <td width="50%" height="124"> 

    @if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) 
    { 
     using (Html.BeginForm("LogOff", "Account")) 
     { 
     <table width="95%" height="86" border="0"> 
        <tr> 
         <td width="50%" valign="middle">Hello, @System.Web.HttpContext.Current.User.Identity.Name</td> 
         <td width="50%" valign="middle" align = "right"><input type="submit" value="Exit" /></td> 
        </tr> 
     </table> 
     } 
    } 
    else 
    { 
      using (Html.BeginForm("LogIn", "Home")) 
      { 
       <table width="95%" height="86" border="0"> 
        <tr> 
         <td width="45%" valign="bottom">Login:</td> 
         <td width="45%" valign="bottom">Password:</td> 
         <td width="10%"></td> 
        </tr> 
        <tr> 
         <td width="45%"> 
          <p> 
           <input type="text" name="login" /> 
          </p> 
         </td> 
         <td width="45%"> 
          <p> 
           <input type="password" name="password" /> 
          </p> 
         </td> 
         <td width="10%" align="left"> 
          <input type="submit" value="Enter" /> 
         </td> 
        </tr> 
        <tr> 
         <td width="45%" valign="top"> 
          @Html.ActionLink("Register", "Register", "Account") 
         </td> 
        </tr> 
       </table> 
      } 

     } 

    </td> 
    </tr> 
</table> 

<hr align="left" width="100%" size="2" /> 

正如你所看到的,這種觀點並不需要是強類型...和我不知道,我是否應該修改任何控制器(因爲所有信息都存儲在HTML上下文中)以及如何修改。我試着打電話給

@Html.RenderAction("RegistrationInfo", "Home"); 

或 @ Html.RenderPartial( 「RegistrationInfo」, 「家」);

每次我收到編譯錯誤包含無效參數應用

回答

0

消息時,下面應該工作:

@Html.Partial("~/Views/Home/RegistrationInfo.cshtml") 

或者如果你是主控制器簡單的視圖中:

@Html.Partial("RegistrationInfo") 

或者,如果你把這個部分在~/Views/Shared/RegistrationInfo.cshtml,你將能夠用速記語法來引用。

+0

非常感謝您 – Eugene

2

雖然達林的回答完全覆蓋你的問題,我想指出的是,註冊信息,好像這是你的佈局更加(母版頁爲.aspx傾斜)不是一個實際的行動的一部分。

所以我會做什麼(事實上,做)被放置在您的佈局的標題部分在右上角或在您的側邊菜單或什麼,並讓佈局處理所有細節渲染它(當你沒有登錄時用一個條件來隱藏它,所以你不會在登錄屏幕上看到它)。

+0

其實,我想給一個可能性讀非註冊用戶這個簡單的博客。但是用戶應該登錄發佈。所以,非註冊用戶會看到用於快速登錄的文本框,註冊用戶會看到問候語。對於AccountConntroller頭應該是有一點不同:只表的左欄....我現在還在思考它 – Eugene

+0

這很好,你仍然可以保留佈局呈現登錄「窗口小部件」,不管是用用戶名+註銷按鈕或「登錄」迷你表單。我的觀點依然存在,它很可能是佈局的一部分,而不是在每個視圖中手動包含部分動作。 – Blindy

+0

明白了。好主意... – Eugene

相關問題