Ajax.Pager not working in MVC4

2019-08-08 02:10发布

问题:

I was using Ajax.Pager in MVC 2 which worked fine.Here is the code in the view

<%= Ajax.Pager(new AjaxOptions { UpdateTargetId = ViewData.Model.UpdateTargetId, OnBegin = "beginPagings", OnSuccess = "successPagings", OnFailure = "failurePaging" },
        ViewData.Model.PageSize, ViewData.Model.PageNumber, ViewData.Model.TotalItemCount, new { controller = ViewContext.Controller.ControllerContext.RouteData.Values["Controller"], action = ViewContext.Controller.ControllerContext.RouteData.Values["action"], Id = ViewContext.Controller.ControllerContext.RouteData.Values["id"], str = ViewContext.Controller.ControllerContext.RouteData.Values["str"] })%>

and this is the AJax link builder code

private MvcHtmlString GeneratePageLink(string linkText, int pageNumber)
        {
            var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
            pageLinkValueDictionary.Add("page", pageNumber);

            return ajaxHelper.ActionLink(linkText, pageLinkValueDictionary["action"].ToString(), pageLinkValueDictionary, ajaxOptions);
        }

But now when I upgrade to MVC 4 this is not generating links as expected.

Here is the MVC 4 code I use in the view

@Ajax.Pager(new AjaxOptions { UpdateTargetId = Model.UpdateTargetId, OnBegin = "beginPagings", OnSuccess = "successPagings", OnFailure = "failurePaging" },
        Model.PageSize, Model.PageNumber, Model.TotalItemCount, new { controller = ViewContext.Controller.ControllerContext.RouteData.Values["Controller"], action = ViewContext.Controller.ControllerContext.RouteData.Values["action"], Id = ViewContext.Controller.ControllerContext.RouteData.Values["id"], str = ViewContext.Controller.ControllerContext.RouteData.Values["str"] })

But the link generated is like below and its no more a link. Its rendered as plain text.

<a data-ajax="true" data-ajax-begin="beginPagings" data-ajax-failure="failurePaging" data-ajax-mode="replace" data-ajax-success="successPagings" data-ajax-update="#divGrid" href="">2</a>

I came across a article which says its because of “unobtrusive Javascript”. Am I missing something??

This issue may be linked to a similar issue in stack overflow

回答1:

This worked by using

@Html.Raw()

@Html.Raw(Ajax.Pager(new AjaxOptions { UpdateTargetId = Model.UpdateTargetId, OnBegin = "beginPagings", OnSuccess = "successPagings", OnFailure = "failurePaging" },
        Model.PageSize, Model.PageNumber, Model.TotalItemCount, new { controller = ViewContext.Controller.ControllerContext.RouteData.Values["Controller"], action = ViewContext.Controller.ControllerContext.RouteData.Values["action"], Id = ViewContext.Controller.ControllerContext.RouteData.Values["id"], str = ViewContext.Controller.ControllerContext.RouteData.Values["str"] }))

Now the text is rendered as links..

Justin's answer helped me..Thanks Justin.