mvc页面导航。我怎么解决这个问题?

时间:2010-11-08 16:48:41

标签: asp.net-mvc

我在ASp.NET Web表单代码中使用了以下代码。它在URL中的传递参数,我所在的页面,以便我制作HTML并添加ON CLASS以显示我所在的页面。现在

我如何在MVC中解决这个问题?任何干净的方法。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Page.IsPostBack))
            {
                string Load;
                Load = "";
                string pagename = Request.Path.Substring(Request.Path.LastIndexOf("http://www.example.com/") + 2);
                if (!(Request.QueryString["Nav"] == null))
                {
                    if (Request.QueryString.Get("Nav") == "Home")
                    {
                        Load = "<div class='menu-image'><img src='images/on-left.gif' alt='' /></div><div class='on'><a href='default.aspx?Nav=Home' class='over'>Home</a></div><div class='menu-image'><img src='images/on-right.gif' alt='' /></div>";
                        Navigate.InnerHtml = Load + "<a href='aboutus.aspx?Nav=About'>About Us</a><a href='contactus.aspx?Nav=Contact'>Contact Us</a>";
                    }
                    else if (Request.QueryString.Get("Nav") == "About")
                    {
                        Load = "<div class='menu-image'><img src='images/on-left.gif' alt='' /></div><div class='on'><a href='aboutus.aspx?Nav=About' class='over'>About Us</a></div><div class='menu-image'><img src='images/on-right.gif' alt='' /></div>";
                        Navigate.InnerHtml = "<a href='default.aspx?Nav=Home' >Home</a>" + Load + "<a href='contactus.aspx?Nav=Contact'>Contact Us</a>";
                    }
                    else if (Request.QueryString.Get("Nav") == "Contact")
                    {
                        Load = "<div class='menu-image'><img src='images/on-left.gif' alt='' /></div><div class='on'><a href='contactus.aspx?Nav=Contact' class='over'>Contact Us</a></div><div class='menu-image'><img src='images/on-right.gif' alt='' /></div>";
                        Navigate.InnerHtml = "<a href='default.aspx?Nav=Home' >Home</a><a href='aboutus.aspx?Nav=About' >About Us</a>" + Load;
                    }
                }
                else if (pagename == "default.aspx")
                {
                    Load = "<div class='menu-image'><img src='images/on-left.gif' alt='' /></div><div class='on'><a href='default.aspx?Nav=Home' class='over'>Home</a></div><div class='menu-image'><img src='images/on-right.gif' alt='' /></div>";
                    Navigate.InnerHtml = Load + "<a href='aboutus.aspx?Nav=About'>About Us</a><a href='contactus.aspx?Nav=Contact'>Contact Us</a>";
                }
                else
                {
                    Navigate.InnerHtml = "<a href='default.aspx?Nav=Home'>Home</a><a href='aboutus.aspx?Nav=About'>About Us</a><a href='contactus.aspx?Nav=Contact'>Contact Us</a>";
                }
            }
        }

2 个答案:

答案 0 :(得分:0)

你可以使用这样的javascript:

<script type="text/javascript">
    $(document).ready(function () {
        var path = location.pathname;
        var home = "/";
        $(".menu a[href='" + [path || home] + "']").addClass("active");
    });
</script>

这通常是您的菜单:

<ul>
    <li><a href="/Home">Item 1</a></li>
    <li><a href="/Help">Item 2</a></li>    
    <li><a href="/About">Item 3</a></li>
    <li><a href="/Contact">Item 4</a></li>
</ul>

答案 1 :(得分:0)

在此处查看我的question以及相应的answer。我在3个不同的MVC项目中使用了这个解决方案。