3

I've tried converting an MVC 2 tutorial webshop to MVC 3 with Razor Syntax, but I don't understand the following problem...

_Layout.cshtml

<div id="header"> 

    <div class="title">SPORTS STORE</div>

</div>

<div id ="categories">
@{Html.RenderAction("Menu", "Nav");}
</div>

"Menu" is an action for a partial View on the "Nav" Controller.

Menu.cshtml

@model IEnumerable<WebShop_1_0.ViewModels.NavLink>

 @{foreach(var link in Model)
 {
        Html.RouteLink(link.Text, link.RouteValues, new Dictionary<string, object>
    {
        { "class", link.IsSelected ? "selected" : null }
    });
 }}

This is the Nav controller

public class NavController : Controller
{
    private IProductsRepository productsRepository;

    public NavController(IProductsRepository productsRepository)
    {
        this.productsRepository = productsRepository;
    }

    public ViewResult Menu(string category)
    {
        // Just so we don't have to write this code twice
        Func<string, NavLink> makeLink = categoryName => new NavLink
        {
            Text = categoryName ?? "Home",
            RouteValues = new RouteValueDictionary(new
            {
                controller = "Products",
                action = "List",
                category = categoryName,
                page = 1
            }),
            IsSelected = (categoryName == category)
        };

        // Put a Home link at the top
        List<NavLink> navLinks = new List<NavLink>();
        navLinks.Add(makeLink(null));

        // Add a link for each distinct category
        //navLinks.AddRange(productsRepository.Products.Select(x => x.Category.Trim()).Distinct().OrderBy(x => x));

        var categories = productsRepository.Products.Select(x => x.Category.Trim());
        foreach (string categoryName in categories.Distinct().OrderBy(x => x))
            navLinks.Add(makeLink(categoryName));

        return View(navLinks);
    }
}

I don't know where the mistake is.

If I use Html.PartialView instead of Html.RenderAction, I get another error message, that VS can't find the PartialView. Most of this is code that I have just copied, just the Views rewritten to MVC 3.

Before this StackOverFlowException problem, the browser would load the webpage for a long time.

This is the routing:

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        /*Sorrend geccire számít*/
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(null, "", // Only matches the empty URL (i.e. ~/)
                        new
                        {
                            controller = "Products",
                            action = "List",
                            category = (string)null,
                            page = 1
                        }
        );
        routes.MapRoute(null, "Page{page}", // Matches ~/Page2, ~/Page123, but not ~/PageXYZ
                        new { controller = "Products", action = "List", category = (string)null },
                        new { page = @"\d+" } // Constraints: page must be numerical
        );
        routes.MapRoute(null, "{category}", // Matches ~/Football or ~/AnythingWithNoSlash
                        new { controller = "Products", action = "List", page = 1 }
        );
        routes.MapRoute(null, "{category}/Page{page}", // Matches ~/Football/Page567
                        new { controller = "Products", action = "List" }, // Defaults
                        new { page = @"\d+" } // Constraints: page must be numerical
        );
        routes.MapRoute(null, "{controller}/{action}");
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactroy());

        ModelBinders.Binders.Add(typeof(Cart), new CartModelBlinder());
    }
}
3

1 Answer 1

19

Your Menu action needs to return a PartialView(navLinks) instead of View(navLinks), otherwise your layout will be drawn with the menu, which causes recursion. Oh oh! This causes the stack overflow :)

3
  • 3
    And must change public ViewResult Menu(string category) to public ActionResult Menu(string category)
    – blaces
    Apr 28, 2011 at 9:34
  • I had this same problem. Is this an error in the book, or is this a change with MVC3 over MVC2?
    – argyle
    Jul 20, 2011 at 0:45
  • Probably an error in the book. I believe "View" has always rendered with layouts.
    – cwharris
    Jul 20, 2011 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.