English 中文(简体)
使用Viewmodel传递数据时发生PagedList错误
原标题:PagedList error when passing Data with Viewmodel

嗨,我想知道是否有人对我在实现页面列表代码时遇到的问题有所了解(https://github.com/TroyGoode/PagedList)在我的asp.net mvc3网站上。以下是我尝试做的事情的细节:

我创建了一个Viemodel,其中包含以下详细信息:

public class ProductViewModelList
{
    public List<Product> ProductBrowse { get; set; }
    public int NumberOfProducts { get; set; } 
    public List<Category> CategoryModel { get; set; } 
}

然后是一个控制器,其中包含我要传递给视图的信息:

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);

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

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>
@using Social.Helpers;
@using System.Web.Mvc.Html

并以这种方式创建带有寻呼机的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>

信息:1-我正在使用

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>

因为我需要将信息从IPagedList和ProductViewModelList传递到View。

2-如果我像<code><;IPagedList<;Social.ViewModels.ProductViewModelList>我收到了IPagedList属性的完整intelligense,但对于Viewmodels NumberofProducts、ProductBrowse、CategoryModel,我只收到它们的名称,而没有收到其他属性,例如ProductBrowse.ProductId。

3- I would like to display a URL of the type: http://www.domain.com/Controller/List?categoryId=2&page=1

4-我不知道我必须做什么才能包含的对象值

Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, ObjectValues for categoryId=2)

Sorry if this is a mess to understand, I tried my best in order to explain. thanks in advance.

最佳回答

而不是:

@inherits System.Web.Mvc.WebViewPage<IPagedList<Social.ViewModels.ProductViewModelList>>

尝试:

@model IPagedList<Social.ViewModels.ProductViewModelList>

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

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

以及~/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>
问题回答

您可以尝试其他库PagedListExt。它可以通过新包装





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签