【问题标题】:ASP.NET MVC 3: StackOverflowException when using PartialViewASP.NET MVC 3:使用 PartialView 时出现 StackOverflowException
【发布时间】:2011-04-27 22:03:41
【问题描述】:

我尝试使用 Razor 语法将 MVC 2 教程网上商店转换为 MVC 3,但我不明白以下问题...

_Layout.cshtml

<div id="header"> 

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

</div>

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

“菜单”是“导航”控制器上部分视图的操作。

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

这是导航控制器

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

我不知道错在哪里。

如果我使用 Html.PartialView 而不是 Html.RenderAction,我会收到另一条错误消息,即 VS 找不到 PartialView。其中大部分是我刚刚复制的代码,只是重写为 MVC 3 的视图。

在出现 StackOverFlowException 问题之前,浏览器会加载网页很长时间。

这是路由:

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());
    }
}

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

您的 Menu 操作需要返回一个 PartialView(navLinks) 而不是 View(navLinks),否则您的布局将与菜单一起绘制,从而导致递归。哦哦!这会导致堆栈溢出:)

【讨论】:

  • 并且必须将public ViewResult Menu(string category)改为public ActionResult Menu(string category)
  • 我遇到了同样的问题。这是书中的错误,还是 MVC3 对 MVC2 的更改?
  • 可能是书中的错误。我相信“视图”总是用布局呈现。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 2012-01-26
相关资源
最近更新 更多