Ajax.Pager not working in MVC4

浪子不回头ぞ 提交于 2019-12-24 10:47:55

问题


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.



来源:https://stackoverflow.com/questions/8956992/ajax-pager-not-working-in-mvc4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!