使用Viewmodel传递数据时出现PagedList错误

s_h*_*s_h 1 c# pagedlist viewmodel asp.net-mvc-3

嗨,我想知道是否有人在我的asp.net mvc3网站上实现页面列表代码(https://github.com/TroyGoode/PagedList)时遇到的一个问题.这是我尝试做的细节:

我用这个细节创建了一个Viemodel:

public class ProductViewModelList
{
    public List<Product> ProductBrowse { get; set; }
    public int NumberOfProducts { get; set; } 
    public List<Category> CategoryModel { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

然后控制器保存我想传递给视图的信息:

public ActionResult List(int categoryid, int? page)
{
    const int defaultPageSize = 20;
    int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
    var categoryModel = db.Category.Include("Product").Single(c => c.CategoryId == categoryid);
    var paginatedmodel = categoryModel.Product.ToPagedList(currentPageIndex, defaultPageSize);
    var viewModel = new ProductViewModelList
    {
        ProductBrowse = paginatedmodel.ToList(),
        NumberOfProducts = categoryModel.Product.Count()
    };
return View(viewModel);
Run Code Online (Sandbox Code Playgroud)

最后以以下内容开头的视图:

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
@using Social.Helpers;
@using System.Web.Mvc.Html
Run Code Online (Sandbox Code Playgroud)

并以这种方式使用寻呼机创建foreach:

@foreach (var item in Model)
                {
                    <div class="result">
<div class="info_result"><h2><a>@Html.ActionLink(Html.TruncateText(item.Title, 25), "Details", new { id = item.ProductId })</a></h2><p><a>@Html.ActionLink(Html.TruncateText(item.Description, 180), "Details", new { id = item.ProductId })</a></p<a>@String.Format("{0:dddd, MMMM d, yyyy}", item.CreatedOn)</a></div>

<div class="paginacion">@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)</div>
Run Code Online (Sandbox Code Playgroud)

信息:1- Im使用

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
Run Code Online (Sandbox Code Playgroud)

因为我需要将信息从IPagedList和ProductViewModelList传递给View.

2-如果我传递了继承,就像<IPagedList<Social.ViewModels.ProductViewModelList>我收到了IPagedList属性的完整智能感知,但对于Viewmodels NumberofProducts,ProductBrowse,CategoryModel我只收到他们的名字,但不是像ProductBrowse.ProductId这样的属性作为例子.

3-我想显示以下类型的URL:http: //www.domain.com/Controller/List?categoryId = 2&page = 1

4-我不知道为了包含对象值我必须做什么

Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, ObjectValues for categoryId=2)
Run Code Online (Sandbox Code Playgroud)

对不起,如果这是一个很难理解,我尽力解释.提前致谢.

Dar*_*rov 5

代替:

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
Run Code Online (Sandbox Code Playgroud)

尝试:

@model IPagedList<Social.ViewModels.ProductViewModelList>
Run Code Online (Sandbox Code Playgroud)

此外,您可以使用显示模板而不是在视图中编写foreach循环.所以在你看来:

<div class="result">
    @Html.DisplayForModel()
</div>
<div class="paginacion">
    @Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)
</div>
Run Code Online (Sandbox Code Playgroud)

在里面 ~/Views/Home/DisplayTemplates/ProductViewModelList.cshtml

@model Social.ViewModels.ProductViewModelList
<div class="info_result">
    <h2>
        @Html.ActionLink(
            Html.TruncateText(Model.Title, 25), 
            "Details", 
            new { id = Model.ProductId }
        )
    </h2>
    <p>  
        @Html.ActionLink(
            Html.TruncateText(Model.Description, 180), 
            "Details", 
            new { id = item.ProductId }
        )
    </p>
    <!-- I've modified this to use a display template instead of String.Format
    Now you only need to decorate your view model property with the DisplayFormat attribute like this:
    [DisplayFormat(DataFormatString = "{0:dddd, MMMM d, yyyy}")]
    public DateTime? CreatedOn { get; set; }
    -->
    @Html.DisplayFor(x => x.CreatedOn)
</div>
Run Code Online (Sandbox Code Playgroud)