Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC 3:使用PartialView时的StackOverflowException_C#_Asp.net Mvc_Asp.net Mvc 3_Razor - Fatal编程技术网

C# ASP.NET MVC 3:使用PartialView时的StackOverflowException

C# ASP.NET MVC 3:使用PartialView时的StackOverflowException,c#,asp.net-mvc,asp.net-mvc-3,razor,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,我尝试过用Razor语法将MVC 2教程webshop转换为MVC 3,但我不理解以下问题 \u Layout.cshtml <div id="header"> <div class="title">SPORTS STORE</div> </div> <div id ="categories"> @{Html.RenderAction("Menu", "Nav");} </div> @model IEnum

我尝试过用Razor语法将MVC 2教程webshop转换为MVC 3,但我不理解以下问题

\u Layout.cshtml

<div id="header"> 

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

</div>

<div id ="categories">
@{Html.RenderAction("Menu", "Nav");}
</div>
@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 }
    });
 }}

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

是否有堆栈跟踪?这可能非常有用,因为StackOverflow可能表示递归。顺便说一句,我相信你问的地方是对的;)尝试
返回PartialView(导航链接)可能重复的,并且必须将
公共视图结果菜单(字符串类别)
更改为
公共操作结果菜单(字符串类别)
我也遇到了同样的问题。这是书中的错误,还是MVC3对MVC2的改变?可能是书中的错误。我相信“视图”总是与布局一起呈现。
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);
    }
}
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());
    }
}