开发者

How to display the similar information in every view?

开发者 https://www.devze.com 2023-04-09 04:20 出处:网络
I would like to display the information about user\'s registration state. It should be displayed on every view, but i consider, that writting this code for about 20 times is a bad idea.By the way, thi

I would like to display the information about user's registration state. It should be displayed on every view, but i consider, that writting this code for about 20 times is a bad idea. By the way, this is the code, it works, i guarantee that:

View/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" />

As you can see, this view doesn't require to be strongly typed... and i don't know, should I modify any controllers (because, all information is stored in HTML Context), and how. I tried to call

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

or @Html.RenderPartial("RegistrationInfo", "Home");

and every time i receive compile error with message about invalid parametres


While Darin's answer perfectly covers your question, I would point out that registration information seems like it's part of your layout more (master page for the .aspx-inclined) than an actual action.

So what I would do (and indeed, do do) is put it either in the header part of your layout in the top right or under your side menu or something, and let the layout handle all the details of rendering it (with a conditional around it to hide it when you're not logged in, so you don't see it on the login screen).


The following should work:

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

Or if you are inside a view of the Home controller simply:

@Html.Partial("RegistrationInfo")

Or if you put this partial in ~/Views/Shared/RegistrationInfo.cshtml you will be able to reference with the shorthand syntax.

0

精彩评论

暂无评论...
验证码 换一张
取 消