简体   繁体   English

Pro Asp.net MVC 2:示例问题

[英]Pro Asp.net mvc 2: Problem in example

I am trying to recreate an example from Chapter-5 of book Pro Asp.net MVC2. 我正在尝试从Pro Asp.net MVC2的第5章中重新创建一个示例。 But as soon as I add Menu code server stops working. 但是,一旦我添加菜单代码服务器就会停止工作。 Any Problem with the code? 代码有问题吗?

public class NavController : Controller
    {
        private IProductRepository productsRepository;
        public NavController(IProductRepository productsRepository)
        {
            this.productsRepository = productsRepository;
        }

        public ViewResult Menu()
        {
            Func<string, NavLink> makeLink = categoryName => new NavLink
            {
                Text = categoryName ?? "Home",
                RouteValues = new RouteValueDictionary( new {
                    controller = "Products", action = "List", category = categoryName, page = 1
                })
            };

            List<NavLink> navLinks = new List<NavLink>();
            navLinks.Add(makeLink(null));

            var categories = productsRepository.Products.Select(x => x.Category);
            foreach (string categoryName in categories.Distinct().OrderBy(x => x))
                navLinks.Add(makeLink(categoryName));

            return View(navLinks);
        }

    }

Menu.cshtml Menu.cshtml

@model IEnumerable<SStore.WebUI.Models.NavLink>

@foreach (var link in Model)
{
    Html.RouteLink(link.Text, link.RouteValues);
}

If I remove this line from my master page then server works 如果我从母版页删除此行,则服务器正常工作

@{
            Html.RenderAction("Menu", "Nav");
        }

otherwise getting this error 否则出现此错误 在此处输入图片说明

Html.RenderAction("Menu", "Nav"); : That's horrible recursion: Nav/Menu which renders Nav/Menu which renders Nav/Menu , ..., until you run out of stack and your web server blows :-) :这是可怕的递归: Nav/Menu呈现Nav/Menu呈现Nav/Menu ...,直到耗尽堆栈并且Web服务器崩溃为止:-)

When you render a child action like this ensure it has no master or the master's gonna rerender it again and again and again, .... So modify this view ( ~/Views/Nav/Menu.cshtml ) like this: 当您渲染这样的子动作时,请确保它没有主控器,或者主控器会一次又一次地重新渲染它,....因此,请像以下那样修改此视图( ~/Views/Nav/Menu.cshtml ):

@model IEnumerable<SStore.WebUI.Models.NavLink>
@{
    Layout = null;
}

@foreach (var link in Model)
{
    Html.RouteLink(link.Text, link.RouteValues);
}

Let me explain: 让我解释:

The example you saw in the book was using the WebForms view engine. 您在本书中看到的示例使用的是WebForms视图引擎。 In this view engine you have .aspx (views) and .ascx (partials). 在此视图引擎中,您具有.aspx (视图)和.ascx (部分)。 I suppose that in the book they were using Menu.ascx which by default has no master because it is a partial. 我想在书中他们使用的是Menu.ascx ,默认情况下它没有母版,因为它是部分母版。

In Razor there is no longer such distinction. 在Razor中不再有这种区别。 You simply have views: .cshtml pages. 您只拥有视图:.cshtml页面。 It is up to you to control whether they have a master or not. 由您决定是否拥有主控。 There are different ways. 有不同的方法。 One is what I showed previously, another is to return PartialView(navLinks) inside the child action. 一个是我之前显示的内容,另一个是在子操作中return PartialView(navLinks)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM