views:

35

answers:

2

In my ASP.Net MVC View, I have a menu. The menu consists of a number of Parent Items, with Child Items (anchor tags) underneath.

I'm using JQuery to toggle the menu items opened and closed, as the user clicks on them.

Unfortunately, once the user clicks onto an anchor tag, and gets sent to another page, the menu state is lost, and the menus go back to being closed.

Now if this was "normal" ASP.Net I'd be sticking the menu state information into the ViewState, and storing the information that way.

Is it possible to do something similar in MVC?? This is the first MVC project that I've worked on, so go gentle with me!

A: 

Maybe this: http://stackoverflow.com/questions/669492/asp-net-mvc-is-there-a-way-to-simulate-a-viewstate

http://blog.maartenballiauw.be/post/2009/10/08/Leveraging-ASPNET-MVC-2-futures-ViewState.aspx
http://forums.asp.net/t/1285163.aspx

and this might help too

http://webcache.googleusercontent.com/search?q=cache:y26JBxHjbBUJ:www.beansoftware.com/ASP.NET-Tutorials/Intro-ASP.NET-MVC.aspx+how+does+mvc+save+view+state&cd=8&hl=en&ct=clnk&gl=us

There are a lot of articles about view states in MVC and since you're new to this perhaps some pre-reading would be handy and may even answer your upcoming questions!

Good luck!

Ehsan
if we still want to stimulate viewstate what's the point in coming to mvc then?
Muhammad Adeel Zahid
I wonder the same thing, so don't ask me, I'm not the one who asked the question. If you like you can post the comment on the question...
Ehsan
To be honest, I'm not really looking to simulate the ViewState. I actually posted because I wanted to discover how people do this kind of thing in a "stateless" environment like MVC. When I get a chance later, I'll be editing the question to make things a little more clear!
NeilD
+1  A: 

So let me first make sure I understand you correctly.

The user will click on a menuitem, which will take him to another page. And when he gets to that page you want the menu to reflect the fact that he is on that page? Correct?

This doesn't sound too tricky. I assume the menu is a partial view - so you would render it something like this:

<div id="menu">
  <% Html.RenderPartial("Menu"); %>
</div> 

So the view will already know which menu item triggered it. For example, if the user clicks on Widgets->New, you might return the NewWidget.aspx view, and it will know that the Menu item to highlight is Widgets->New. So you simply use the overload for RenderPartial to specify the name or id of the menu item to highlight.

<div id="menu">
  <% Html.RenderPartial("Menu", "newWidgetLink"); %>
</div> 

If it is NOT the case that a view already knows which Menu Item to highlight, you will need to pass the id of the menu item with the link. So your link generation will look something like this:

Html.ActionLink("Menu Item Text", 
                "Controller name goes here",
                "Action name goes here",
                new { menuItem = "menuItemId goes here" },
                null
                )

Then your action will need to handle this parameter. Easiest would be to take the parameter and add it to the ViewData. The Menu will then check in the ViewData for the Id of the MenuItem to highlight.

Jaco Pretorius