I’m trying to convert a small mvc2 application to the mvc3 razor syntax. In my mvc2 application I’m using the aspx view engine with a master page. Following the example from Steven Sanderson’s Pro MVC2 book 2nd edition, in the masterpage I call a controller action that renders a partial view for each entity. This is working correctly.
<div id="categories">
<% Html.RenderAction("Menu", "Nav"); %>
</div>
using _layout.cshtml and razor I’m trying this. Here is where my problem comes in.
<div id="categories">
@{
Html.RenderAction("Menu", "Nav");
}
</div>
This is causing an infinite loop now and I’m getting oddly enough a StackOverflowException. Can anyone help me correct the problem? Here is the controller method code.
public ViewResult Menu(string personId)
{
Func<string, NavLink> makeLink = pId => new NavLink
{
Text = pId ?? "Home"
, RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person"})
};
List<NavLink> navLinks = new List<NavLink> {makeLink(null)};
Func<Person, NavLink> makeLink2 = p => new NavLink
{
Text = p.Name ?? "Home"
, RouteValues = new RouteValueDictionary(new { controller = "Person", action = "Person", personId = p.Id })
};
var people = usersRepository.People.OrderBy(x => x.Name);
var peopleLinks = EnumerableHelpers.MakeLinks(people, makeLink2);
navLinks.AddRange(peopleLinks);
return View("_menu", navLinks);
}
Any help or tips is most appreciated.
Thanks,
~ck in San Diego
You didn’t post the actual stack trace, but from the description I’m guessing your recurssion is in the ‘partial’ action view running the layout page, which renders the action, which renders the layout, etc.
Try returning a
PartialViewfrom your child action method instead of aView. This will prevent the _ViewStart page from being executed which will prevent the layout from being rendered for your child action. More discussion about this is here: http://forums.asp.net/t/1624687.aspx