【发布时间】: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());
}
}
【问题讨论】:
-
任何堆栈跟踪?这可能真的很有帮助,因为 StackOverflow 可能表示递归。顺便说一句,我相信你在正确的地方问;)
-
试试
return PartialView(navLinks);
标签: c# asp.net-mvc asp.net-mvc-3 razor