c# - PagedList error when passing Data with Viewmodel -
hi wondering if con shed light problem i´m experiencing when implementing pagedlist code (https://github.com/troygoode/pagedlist) in asp.net mvc3 website. details of i´m trying do:
i created viemodel details:
public class productviewmodellist { public list<product> productbrowse { get; set; } public int numberofproducts { get; set; } public list<category> categorymodel { get; set; } }
then controller holding information want pass view:
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);
finally view starts with:
@inherits system.web.mvc.webviewpage<ipagedlist<social.viewmodels.productviewmodellist>> @using social.helpers; @using system.web.mvc.html
and create foreach pager in way:
@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>
information: 1- im using
@inherits system.web.mvc.webviewpage<ipagedlist<social.viewmodels.productviewmodellist>>
because need pass information ipagedlist , productviewmodellist view.
2- if pass inherits <ipagedlist<social.viewmodels.productviewmodellist>
received complete intellisense ipagedlist properties viewmodels numberofproducts, productbrowse, categorymodel receive names not ther properties productbrowse.productid example.
3- display url of type: http://www.domain.com/controller/list?categoryid=2&page=1
4- dont know have in order include object values
html.pager(model.pagesize, model.pagenumber, model.totalitemcount, objectvalues categoryid=2)
sorry if mess understand, tried best in order explain. in advance.
instead of:
@inherits system.web.mvc.webviewpage<ipagedlist<social.viewmodels.productviewmodellist>>
try:
@model ipagedlist<social.viewmodels.productviewmodellist>
also instead of writing foreach loops in views use display template. in view:
<div class="result"> @html.displayformodel() </div> <div class="paginacion"> @html.pager(model.pagesize, model.pagenumber, model.totalitemcount) </div>
and inside ~/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 use display template instead of string.format need decorate view model property displayformat attribute this: [displayformat(dataformatstring = "{0:dddd, mmmm d, yyyy}")] public datetime? createdon { get; set; } --> @html.displayfor(x => x.createdon) </div>
Comments
Post a Comment