learning
learning

Reputation: 11725

layout difficulty in MVC3

I am having a login page. In the login Page I don`t have any menu and based upon the user login a menu appear on the Home page.

My problem is how not to display the menu only in my login page?

I am having a page MenuControlPartial.cshtml as follows for the menu:

 <li><a href="#">Admin</a>
                  <ul>
                <li>TimeKeeper</li>
                <li>AAA</li>
                <li>BBB</li>
                <li>CCC</li>
                </ul>
                </li>


                <li><a href="#">Settings</a>
                <ul>
                <li>VV</li>   
                <li>XX</li>                    
                </ul>
                </li>
            </ul>

My _layout.cshtml is as as follows:

 <div id="page">
            <div id="header">
                <div id="title">
                                <br />
                </div>
                @if (Request.IsAuthenticated)
                {    
                    <div id="loginInfo">
                    @Html.Partial("_LogOnPartial")
                    </div>

                    <div class="clear">
                    </div>
                       <div id="menucontainer">
                        @Html.Partial("MenuControlPartial")
                        </div>
                    <div class="clear"></div>
                }
                </div>
            <div id="content">  
            @RenderBody() 
            </div>
    </div>
                }
    </body>

Upvotes: 0

Views: 655

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

You could test the current controller and action

@if (!(Html.ViewContext.RouteData.GetRequiredString("controller") == "Login" && Html.ViewContext.RouteData.GetRequiredString("action") == "Index")) {
    <div id="menucontainer">
        @Html.Partial("MenuControlPartial")
    </div>
}

And to avoid this ugliness write a helper:

public static bool ShouldDisplayMenu(this HtmlHelper htmlHelper)
{
    var routeData = htmlHelper.ViewContext.RouteData;
    var controller = routeData.GetRequiredString("controller");
    var action = routeData.GetRequiredString("action");
    return  !(controller == "Login" && action == "Index");
}

and then:

@if (Html.ShouldDisplayMenu()) {
    <div id="menucontainer">
        @Html.Partial("MenuControlPartial")
    </div>
}

Upvotes: 3

Related Questions