在 ASP.NET MVC 中更改所选选项卡图像的最简单方法是什么?

发布于 2024-07-23 12:48:22 字数 325 浏览 2 评论 0原文

我见过这个问题,设置活动选项卡的简单方法,但是我不确定这是否是最佳解决方案。

我想看看其他人如何在 ASP.NET MVC 的视图/控制器中处理选项卡选择。

在 ASP.NET MVC 中实现可选选项卡的最简单方法是什么? 我想避免使用 javascript 方法,以允许未启用 js 的浏览器仍然看到所选选项卡。

I've seen this question, An easy way to set the active tab , however I'm not sure this is an optimal solution.

I'm looking to see how others have handled tab selection in their views/controllers in ASP.NET MVC.

What is the simplest way to implement selectable tabs in ASP.NET MVC? I'd like to avoid javascript methods to allow for non-js enabled browsers to still see the selected tab.

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

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

发布评论

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

评论(2

轻拂→两袖风尘 2024-07-30 12:48:22

编辑:从上面开始,这不是我所做的,这是我使用的两种方法的非工作组合。

我必须处理一页上两个不同位置的状态。 第一种方法是我将 CSS 添加到视图中以将选项卡更改为正确的样式。 我要做的第二件事是使用 ViewData 给我一个变量,我将其传递给我编写的帮助程序,以在视图的头部分输出 CSS,以指示特定于页面的状态(如果这有意义的话,有点像一个类别将在产品页面上突出显示)。

所以在我看来,我有这些台词:

<% var onState = ViewData["OnState"].ToString(); %>
<%= Html.OutputOnState(onState) %>

这很好地实现了我所追求的目标。 CSS 被硬编码到助手中,因为这是一个小站点,所以我确信有更好的方法来做到这一点。

EDIT: Scratch that above, that's not what I did, that was a non-working combination of two methods I used.

I've had to handle on states in two different places on one page. The first way I just added CSS to the view to change the tab to the correct style. The second thing I had to do was use ViewData to give me a variable that I passed to a helper I wrote to output CSS in the head section of the view to indicate a page-specific on state (if that makes sense, kind of like a category would be highlighted on a page of products).

So in my view I have these twho lines:

<% var onState = ViewData["OnState"].ToString(); %>
<%= Html.OutputOnState(onState) %>

That accomplishes what I'm after very well. The CSS is hard-coded into the helper since this is a small site, so I'm sure there is a better way to do this.

不喜欢何必死缠烂打 2024-07-30 12:48:22

我在简化的示例中做了类似于以下的操作;-)

控制器:

public ActionResult Index()
{
    ViewData["MainMenuOn"] = "Home";
    return View();
}

public ActionResult About()
{
    ViewData["MainMenuOn"] = "About";
    return View();
}

public ActionResult Contact()
{
    ViewData["MainMenuOn"] = "Contact";
    return View();
}

视图:

<ul id="main-menu">
    <li class="<%= Html.MainMenuOn("Home") %>">... action link for home action ...</li>
    <li class="<%= Html.MainMenuOn("About") %>">... action link for about action ...</li>
    <li class="<%= Html.MainMenuOn("Contact") %>">... action link for contact action ...</li>
</ul>

帮助程序扩展:

public static string MainMenuOn(this HtmlHelper html, string menuItem)
{
    if ((html.ViewData["MainMenuOn"] != null) && (html.ViewData["MainMenuOn"] == menuItem))
        return "on";

    return "";
}

那么你的 css 应该相当简单

ul#main-menu li
{
    background: green;
}

ul#main-menu li.on
{
    background: blue;
}

有很多方法可以给这只猫换皮...根据你的需要,我会开始寻找不同的方法实现 HtmlHelper 扩展。

HTH,
查尔斯

I do something similar to the following over simplified example ;-)

Controller:

public ActionResult Index()
{
    ViewData["MainMenuOn"] = "Home";
    return View();
}

public ActionResult About()
{
    ViewData["MainMenuOn"] = "About";
    return View();
}

public ActionResult Contact()
{
    ViewData["MainMenuOn"] = "Contact";
    return View();
}

View:

<ul id="main-menu">
    <li class="<%= Html.MainMenuOn("Home") %>">... action link for home action ...</li>
    <li class="<%= Html.MainMenuOn("About") %>">... action link for about action ...</li>
    <li class="<%= Html.MainMenuOn("Contact") %>">... action link for contact action ...</li>
</ul>

Helper extension:

public static string MainMenuOn(this HtmlHelper html, string menuItem)
{
    if ((html.ViewData["MainMenuOn"] != null) && (html.ViewData["MainMenuOn"] == menuItem))
        return "on";

    return "";
}

Then your css should be rather simple

ul#main-menu li
{
    background: green;
}

ul#main-menu li.on
{
    background: blue;
}

There are many ways to skin this cat... depending on your needs I would start looking at different ways to implement the HtmlHelper extension.

HTHs,
Charles

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