ASP.Net MVC 5 Pager number change language

2019-08-11 15:33发布

问题:

I have been using ASP.Net MVC 5 to build my web application, and for pagination purpose I followed the below tutorial.

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

Everything works out as expected. But my project requires the page list shown in pager to be in Bengali instead of English, depending on the culture information.

E.g. I have the following in my page

1 2 3

But I need it to be

so the numbers are translated.

Is there a way to achieve that using the PagedList.MVC component I used?

回答1:

You can use PagedListRenderOptions to specify a function to format your numbers. Example adapted from documentation on GitHub:

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfProducts,
    page => Url.Action("Index", new { page = page }),
    new PagedListRenderOptions {
        FunctionToDisplayEachPageNumber =
            page => page.ToString()
    }
)

Key point is this instructions: page => page.ToString(), default implementation simply does String.Format(LinkToIndividualPageFormat , page) but you can replace it with your own function (like in previous example).

Unfortunately page.ToString(new CultureInfo("bn-IN")) won't print 1 as so you have to do it by hand. A very naive example (do not use this, it's terrible and inefficient) just to explain what I mean:

page => page.ToString()
    .Replace("1", "\u09e7") 
    .Replace("2", "\u09e8")
    .Replace("3", "\u09e9"); // And so on...