开发者

mvc page navigation. How I solve this?

I used below code in ASp.NET web forms code behined. Its passing parameter in the URL to which which page I am on so that I make HTML and add ON CLASS to show the page I am on. Now

How I solve this in MVC?. Any clean approach.

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>";
                }
            }
        }


Check out my question here, and the appropriate answer. I have used this solution in 3 different MVC projects.


you can use javascript like this:

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

And this would typically be your menu:

<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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜