MVC3 _Layout.cshtml...禁用/启用菜单项

发布于 2024-12-09 09:19:59 字数 124 浏览 0 评论 0原文

我有一个 MVC3 C#.Net 项目。当我最初加载我的网站时,我想路由到索引页面并禁用屏幕顶部的导航选项卡,直到做出选择。一旦做出选择,站点就会转到详细信息页面。然后我想启用导航菜单项。我该怎么做?传递 ViewBag 吗?没有把握

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔戏命师 2024-12-16 09:19:59

我认为问题在于索引页面不遵循网站其余部分的布局,因此索引页面不应使用主布局。

在索引页上:

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

<p>Your content below</p>

如果您想在某些条件下呈现菜单,则将菜单存储在部分视图模型中,例如 SiteNavigation.cshtml

然后您可以根据某些条件呈现此视图。例如

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

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"); }
}
谁的年少不轻狂 2024-12-16 09:19:59

我找到了我的问题的答案。 @CodeRush 的答案很酷,我将在另一个场景中使用它。但我喜欢下面针对我的特殊情况的实现。在 Object1Controller Index get Action 中,我添加了以下代码:

ViewBag.Hidden = "hidden";

然后在 Details get Action 中,即在 Index 视图上单击链接时调用的操作,我添加了以下代码:

ViewBag.Hidden = "visible";

然后在 _Layout.cshtml 中添加了以下代码:

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

呈现为:

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

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

在其他控制器中,我不需要设置 ViewBag.Hidden 属性,因为可见性属性的默认值是“可见”。对于其他控制器,ViewSource 显示:

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

哪个呈现为可见

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文