時,我已經試過了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());
}
}
任何堆棧跟蹤?這可能真的有幫助,因爲StackOverflow可能指示遞歸。順便說一句,我相信你問的是在正確的地方;) – 2011-04-27 22:11:39
嘗試'返回PartialView(navLinks);' – driushkin 2011-04-27 22:52:50
[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