.net - PagedList very redundant and repetive code -
i have pagedlist on page , allows 5 pages view results results in format www.example.com/viewing?1 ... 2 ...3 way 5 , checking page on doing
@if (model.article.pagenumber == 1) { }
then
@if (model.article.pagenumber == 2) { }
all way 5 .. there improve way here code..
// @ model.article.pagenumber can see utilize pages 1-5 , // keeps tabs in order if page=2 first element page2 -1 , // how can cut down code can add together page numbers correctly without repeating 5 times @if (model.article.pagenumber == 1) { @html.actionlink(string.format("{0}", (model.article.pagenumber).tostring()), "index", new { page = model.article.pagenumber }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 1).tostring()), "index", new { page = model.article.pagenumber + 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 2).tostring()), "index", new { page = model.article.pagenumber + 2 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 3).tostring()), "index", new { page = model.article.pagenumber + 3 })@html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 4).tostring()), "index", new { page = model.article.pagenumber + 4 }) } @if (model.article.pagenumber == 2) { @html.actionlink(string.format("{0}", (model.article.pagenumber - 1).tostring()), "index", new { page = model.article.pagenumber - 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber).tostring()), "index", new { page = model.article.pagenumber }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 1).tostring()), "index", new { page = model.article.pagenumber + 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 2).tostring()), "index", new { page = model.article.pagenumber + 2 })@html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 3).tostring()), "index", new { page = model.article.pagenumber + 3 }) @html.raw(" "); } @if (model.article.pagenumber == 3) { @html.actionlink(string.format("{0}", (model.article.pagenumber - 2).tostring()), "index", new { page = model.article.pagenumber - 2 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 1).tostring()), "index", new { page = model.article.pagenumber - 1 })@html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber).tostring()), "index", new { page = model.article.pagenumber })@html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 1).tostring()), "index", new { page = model.article.pagenumber + 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 2).tostring()), "index", new { page = model.article.pagenumber + 2 }) } @if (model.article.pagenumber == 4) { @html.actionlink(string.format("{0}", (model.article.pagenumber - 3).tostring()), "index", new { page = model.article.pagenumber - 3 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 2).tostring()), "index", new { page = model.article.pagenumber - 2 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 1).tostring()), "index", new { page = model.article.pagenumber - 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber).tostring()), "index", new { page = model.article.pagenumber }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber + 1).tostring()), "index", new { page = model.article.pagenumber + 1 }) @html.raw(" "); } @if (model.article.pagenumber == 5) { @html.actionlink(string.format("{0}", (model.article.pagenumber - 4).tostring()), "index", new { page = model.article.pagenumber - 4 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 3).tostring()), "index", new { page = model.article.pagenumber - 3 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 2).tostring()), "index", new { page = model.article.pagenumber - 2 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber - 1).tostring()), "index", new { page = model.article.pagenumber - 1 }) @html.raw(" "); @html.actionlink(string.format("{0}", (model.article.pagenumber).tostring()), "index", new { page = model.article.pagenumber }) }
since limiting 5 pages, why footer or whatever need dynamically generated based on current page number big if/else statement? right in unruly quickly, if decide add together more pages in future article.
it seems pass single property such record count or numpages , following:
@{for(var pagenum = 1; pagenum <= model.article.numpages, pagenum++){ @html.actionlink(string.format("{0} ", pagenum), "index", new { page = pagenum }) }}
this cut down nesting see above, removes need html.raw @ end , page agnostic (meaning doesn't matter page on). include current requested page in view model not create page link. example:
@{for(var pagenum = 1; pagenum <= model.article.numpages, pagenum++){ if(pagenum <> model.article.pagenumber){ @html.actionlink(string.format("{0} ", pagenum), "index", new { page = pagenum }) }else{ @model.article.pagenumber } }}
.net asp.net-mvc-3 html-helper code-duplication pagedlist
No comments:
Post a Comment