首页 新闻 会员 周边

求助,看过pro mvc3的大湿

0
[已关闭问题] 关闭于 2012-09-06 16:42

这是在学校Pro Mvc3第八章时候出现的一个不明白的地方

在第八章,运行的时候单从路由看,会执行Product控制器的List方法

 public ViewResult List(string category, int page = 1)
        {
            ProductsListViewModel viewModel = new ProductsListViewModel()
                    {
                        Products = repository.Products
                                .Where(p => category == null || p.Category == category)
                                .OrderBy(p => p.ProductID)
                                .Skip((page - 1) * PageSize)
                                .Take(PageSize),
                        PagingInfo = new PagingInfo
                                        {
                                            CurrentPage = page,
                                            ItemsPerPage = PageSize,
                                            TotalItems = category == null ?
                                                  repository.Products.Count() :
                                                  repository.Products.Count(e => e.Category == category)
                                        },
                        CurrentCategory = category
                    };
            return View(viewModel);
        }

   在第一次访问的时候,category=null;page=1。这些数据会赋值给ProductsListModel.然后传给List视图经行渲染。

@model SportsStore.WebUI.Models.ProductsListViewModel
@{
    ViewBag.Title = "Products";
}
@foreach (var p in Model.Products)
{
    Html.RenderPartial("ProductSummary", p);
}
<div class="pager">
    @Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new { page = x, category = Model.CurrentCategory }))
</div>

    List视图会加载ProductSummary视图,把Product传给该视图进行渲染。在返回该视图的时候会合并Layout视图。
    

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.7.2.min.js")" type="text/javascript"></script>
</head>
<body>
    <div id="header">
        <div class="title">
            SPORT STORE</div>
    </div>
    <div id="categories">
        @{
            Html.RenderAction("Menu", "Nav");
        }
    </div>
    <div id="content">
        @RenderBody()
    </div>
</body>
</html>

    Lyaout视图会通过RenderAction去通过访问NavController的Menu来加载导航视图。

public PartialViewResult Menu(string category = null)
        {
            ViewBag.SelectedCategory = category;

            IEnumerable<string> categories = repository.Products
                .Select(x => x.Category)
                .Distinct()
                .OrderBy(x => x);
            return PartialView(categories);
        }

    Menu方法有个Category参数,这个参数来指示那个导航是被选中的,通过ViewBage传到视图当中去。

@model IEnumerable<string>
@{
    Layout = null;
}
@Html.ActionLink("Home", "List", "Product")
@foreach (var link in Model)
{
    @Html.RouteLink(link,
                 new { controller = "Product", action = "List", category = link},
                 new { @class = link == ViewBag.SelectedCategory ? "selected" : null }
                )
}

    但是当我点击一个分类的时候,比方Chese类别,这个导航的href="/Chess"。从路由来看,这个url相当于/Product/List?Categry=Chese?Page=1
    上面的url返回的是List视图,category=Chese会保存到视图模型ProductsListModel当中去,然后合并Layout,关键是Layout中的Html.RenderAction("Menu","Nav“)去Render这个Action的时候并没有把导航Chese这个值传过去,但是在NavController的Menu却能接受到这个值,是我对RenderAction不够了解吗?Menu怎么就能获取category这个参数值的。

凡一二三的主页 凡一二三 | 初学一级 | 园豆:85
提问于:2012-06-15 15:01
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册