How to make a pager with MVCContrib and Razor?

2019-07-03 12:53发布

问题:

I have a pager as follows:

@using MvcContrib.UI.Pager
@model MvcContrib.Pagination.IPagination

<p/>
  @Html.Pager(Model).First("First").Last("Last").Next("Next").Previous("Previous")
<p/>

Instead of displaying this:

Showing 1 - 10 of 10841 First | Previous | Next | Last

It displays this:

<div class='pagination'><span class='paginationLeft'>Showing 1 - 10 of 10841 </span><span class='paginationRight'>First | Previous | <a href="/Home/Events?page=2">Next</a> | <a href="/Home/Events?page=1085">Last</a></span></div>

I also downloaded a sample project from codeproject, but when I run it, I get the same problem.

What could possibly be the problem? Can you guys help me out?

回答1:

Razor automatically encodes html if you return a String. It won't encode Html if you return an IHtmlString.

Does the pager method return a String instead of IHtmlString?

Try using Html.Raw. This method will convert a String to an IHtmlString.

@Html.Raw(Html.Pager(Model).First("First").Last("Last").Next("Next").Previous("Previous"))


回答2:

The reason it works in the sample project and not your project is because in the sample project they're using the @Html.Pager in a partial page which is then called on the main page using @{Html.RenderPartial();} when rendering like that the rendered string from the Pager is output as html rather than encoded html.

If you need to use the pager without the subpages then you'll need to wrap the call in Html.Raw as Linkgoron suggested as the Html.Pager defaults to using the ToString which returns a string and not IHtmlString