开发者

MVC3 _Layout.cshtml...disableing/enabling menu items

开发者 https://www.devze.com 2023-04-12 10:30 出处:网络
I have an MVC3 C#.Net project.When I initially load my Web site, I want to route to an Index 开发者_如何转开发page and have the navigation tabs at the top of the screen be disabled until a selection i

I have an MVC3 C#.Net project. When I initially load my Web site, I want to route to an Index 开发者_如何转开发page and have the navigation tabs at the top of the screen be disabled until a selection is made. Once a selection is made, the site routes to a details page. I would then like to enable the navigation menu itmes. How can I do this? Pass a ViewBag around? Not sure


I think the problem is that Index page doesn't follow a layout of the rest of the website, therefore index page shouldn't use a master layout.

On your index page:

@{
    ViewBag.Title = "Index page";
    // Null or a custom layout
    Layout = null;
}

<p>Your content below</p>

If you want to render a menu on some condition, then store menu in a partial view model, e.g. SiteNavigation.cshtml

You can then render this view based on some condition. E.g.

@if(true){
   @{ Html.RenderPartial("SiteNavigation"); }
}


I found an answer to my question. @CodeRush's answer is cool, which I will use in another scenario. But I like the implementation, below, for my prticular situation. In the Object1Controller Index get Action, I added the below code:

ViewBag.Hidden = "hidden";

Then in the Details get Action, which is the Action called when a link is clicked on the Index view, I added this code:

ViewBag.Hidden = "visible";

Then in the _Layout.cshtml I added this code:

 <li style="visibility: @ViewBag.Hidden">@Html.ActionLink("About", "About", "Home")</li>

Which renders as:

<li style="visibility: hidden"><a href="/Home/About">About</a></li>

or

<li style="visibility: visible"><a href="/Home/About">About</a></li>

In the other Controllers, I don't need to set the ViewBag.Hidden property since the default for the visibility attribute is "visible". For the other Controllers, the ViewSource shows:

<li style="visibility: "><a href="/Home/About">About</a></li>

Which renders as visible

0

精彩评论

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