asp.net mvc3 和多级导航

发布于 2024-10-28 21:22:52 字数 1819 浏览 3 评论 0原文

我正在编写新的 ASP.NET MVC 应用程序,我对创建多级导航系统有疑问。

例如,我有带有主导航(Cpu --- Gpu ---- Ram)的网络

和带有(intel,amd --- ati,nvidia --- DDR2,DD3)的子导航

那么,我的第一个实现在这里:

public class NavigationItem
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Controller { get; set; }
    public virtual string Action { get; set; }
    public virtual string Url { get; set; }
    public virtual string Section { get; set; }
}


public class NavigationController : Controller
{
    private readonly IUnitOfWork _unitOfWork;

    public NavigationController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public ActionResult MainMenu()
    {
        return View(_unitOfWork.NavigationItems.Where(x => x.Section == null).ToList());
    }

    public ActionResult SectionMenu()
    {


        return View(_unitOfWork.NavigationItems.Where(x => x.Section == "// name of section").ToLis());
    }
}

最后我的布局页面是:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
 <div class="main-menu">
  @{Html.RenderAction("MainMenu", "Navigation");}
 </div>

 <div class="section-menu">
  @{Html.RenderAction("SectionMenu", "Navigation");}
 </div>

    @RenderBody()
</body>
</html>

通过这个实现,我遇到了如何在 MainMenu 处于活动状态时在SectionMenu 中处理的问题,因为我想生成依赖于MainMenu 的SectionMenu 并突出显示它们。

我认为女巫的一种解决方法是在SectionMenu 中处理url(控制器)。 例如:

    if (RouteData.Values["controller"].ToString() == "Administration")
    {
        // Generate section menu for Administration main menu
    }

我不喜欢这个解决方案,因为我使用“魔术字符串”值,并且每个主菜单只有一个控制器。

如何实施这个解决方案?

感谢您的建议

i am writing new asp.net mvc application and i have question about creating multi-level navigation system.

For example i have web with main navigation (Cpu --- Gpu ---- Ram)

and sub-navigation with (intel,amd --- ati,nvidia --- DDR2,DD3)

Well, my first implementation is here :

public class NavigationItem
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Controller { get; set; }
    public virtual string Action { get; set; }
    public virtual string Url { get; set; }
    public virtual string Section { get; set; }
}


public class NavigationController : Controller
{
    private readonly IUnitOfWork _unitOfWork;

    public NavigationController(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public ActionResult MainMenu()
    {
        return View(_unitOfWork.NavigationItems.Where(x => x.Section == null).ToList());
    }

    public ActionResult SectionMenu()
    {


        return View(_unitOfWork.NavigationItems.Where(x => x.Section == "// name of section").ToLis());
    }
}

And finally my layout page is :

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
</head>
<body>
 <div class="main-menu">
  @{Html.RenderAction("MainMenu", "Navigation");}
 </div>

 <div class="section-menu">
  @{Html.RenderAction("SectionMenu", "Navigation");}
 </div>

    @RenderBody()
</body>
</html>

With this implementation i have problem how i handle in SectionMenu with MainMenu is active, because i want generate SectionMenu depends on MainMenu and highlight them.

One workaround of witch i think is handle url(controller) in SectionMenu.
For example :

    if (RouteData.Values["controller"].ToString() == "Administration")
    {
        // Generate section menu for Administration main menu
    }

I dont like this solution because i work with "magic string" values and i havent only one controller per one MainMenu.

How implement this solution?

Thanks for advice

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

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

发布评论

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

评论(2

╰沐子 2024-11-04 21:22:52

我想你必须更深入地了解 MVC 的路由系统。一开始看起来不太简单,但这可能是整个 MVC 中最重要的区域。有时,您很容易感到困惑,尤其是当您在 Global.asax 中定义了许多路由时。有一个名为 Route debugger 的项目,它很有帮助。我不记得链接了。尝试用谷歌搜索它...如果找不到它我稍后会发送链接...

I guess you have to get deeper into MVC's routing system. It doesn't look very straightforward in the beginning, but this is probably the most important area of the entire MVC. Sometimes though you can easily get confused especially if you have many routes defined in Global.asax. There is a project called Route debugger, it helps. I don't remember the link. Try to google it... If you can't find it I'll send the link later...

家住魔仙堡 2024-11-04 21:22:52

好的,我使用 ViewBag 功能。不干净,但有效

Ok, I use ViewBag feature. Not clean, but works

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