I then used Html.RenderAction in the Layout page to render the menu. To my surprise, I got a stack overflow. Upon debugging i realized that it was looping over the action method again and again.
It turns out...and I'm assuming this is Razor specific but have no idea... that if you return a ViewResult from your action method, it will reference the Layout page and call the RenderAction method again. Which returns another ViewResult and references the Layout page again. And again. And again. Until it causes a crash.
The solution was to return a PartialViewResult from the Menu() action, instead of ViewResult. This eliminated the infinite loop and correctly rendered my menu.
10 comments:
Thanks, I had a near identical problem that showed up when code was ported from ASP.NET MVC 2 to MVC 3 and switched to the Razor view engine.
Thanks! that solved my problem.
It appeared when I was trying to write Sanderson's Sports Store example in MVC3. I'm glad you did it before me.
Thank you very much! I was trying to render a menu just like in the SportsStore example, where I ran into the same problem. Thanks for solving. I think I would never figure out what to do.
Not working for my.
The menu not appears
Ok
I was working with:
@foreach (var link in Model) {
@Html.RouteLink(link.Text, link.RouteValues)
}
in the Menu.cshtml, but the menu appears in blank. I add the < div > following:
@foreach (var link in Model) {
< div >@Html.RouteLink(link.Text, link.RouteValues)< /div >
}
And Working!! YEA!
Thank you for posting this. It was driving me crazy trying to figure out what was going on.
Thanks so much! I was having the exact same issue
Thanks for the tip! Just saved me the good deal of time it would have taken for me to figure that out!
Thanks for the comments glad to help. I've been doing heavy MVC 3 development for the last 8 months I will try to post some more helpful items in the future now that I see they might actually be useful to someone.
I also had issues with this and this solved it. However I also had the issue Nicolas Andres had. My solution is to add @ infront of Html.RenderAction. I.e.:
@Html.RouteLink(link.Text, link.RouteValues);
Post a Comment