2011-04-27 69 views
3

時,我已經試過了MVC 2教程網店轉換爲MVC 3剃鬚刀語法,但我不明白以下問題......ASP.NET MVC 3:StackOverflowException使用PartialView

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

任何堆棧跟蹤?這可能真的有幫助,因爲StackOverflow可能指示遞歸。順便說一句,我相信你問的是在正確的地方;) – 2011-04-27 22:11:39

+1

嘗試'返回PartialView(navLinks);' – driushkin 2011-04-27 22:52:50

+0

[ASP.NET MVC堆棧溢出異常時從母版頁調用部分視圖](http:// stackoverflow.com/questions/2814824/asp-net-mvc-stack-overflow-exception-when-calling-a-partial-view-from-master-pag) – 2016-11-16 18:32:18

回答

18

你的菜單操作需要返回PartialView(navLinks)而不是視圖(navLinks),否則你的佈局將與菜單,這會導致遞歸繪製。哦,哦!這會導致堆棧溢出:)

+0

是的,謝謝大家! – blaces 2011-04-28 09:33:23

+3

並且必須將'public ViewResult Menu(string category)'更改爲'public ActionResult Menu(string category)' – blaces 2011-04-28 09:34:05

+0

我有同樣的問題。這是本書中的錯誤,還是MVC3與MVC2的改變? – jeromeyers 2011-07-20 00:45:16

相關問題