gpt4 book ai didi

c# - ASP.NET MVC 3 : StackOverflowException when using PartialView

转载 作者:行者123 更新时间:2023-11-30 13:12:27 26 4
gpt4 key购买 nike

我已尝试使用 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>

“菜单”是“导航” Controller 上部分 View 的操作。

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

这是导航 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);
}
}

不知道错在哪里

如果我使用 Html.PartialView 而不是 Html.RenderAction,我会收到另一条错误消息,即 VS 找不到 PartialView。其中大部分是我刚刚复制的代码,只是将 View 重写为 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());
}
}

最佳答案

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

关于c# - ASP.NET MVC 3 : StackOverflowException when using PartialView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5811172/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com