用MVCpager的Ajax分页,会出现多个相同的请求,请求的内容也相同。见下图
返回的内容为mvcpager生成的js.最后有代码。
怎么会出现这么多相同的请求,肯定会影响性能,失去了用Ajax的意义。
问大神:
1.为什么产生这么多相同的请求?
2.怎么去避免?
谢谢
view中:
@{Html.RenderPartial("~/Views/Comment/FunnyComment.cshtml", ViewData["list"]);}
PartialView(FunnyComment.cshtml)
@Ajax.Pager(Model, "comment", new {id=ViewBag.FunnyID }, new PagerOptions { PageIndexParameterName = "pageindex", ShowPageIndexBox = true, PageIndexBoxType = PageIndexBoxType.DropDownList, ShowGoButton = false }, new MvcAjaxOptions { UpdateTargetId = "comment", LoadingElementId = "loadingDiv", OnBegin = "$('#comment').fadeOut('slow')", OnComplete = "$('#comment').fadeIn('slow')" }, new { @class = "pager" }) @{Html.RegisterMvcPagerScriptResource();}
Controller:
public ActionResult Details(int id, int pageindex = 1) { List<Comment> list; using (ICommentBusiness iComment = new CommentBusiness(new JustSayEntities())) { list = iComment.LoadPageEntities(2, pageindex, new Func<Comment, bool>(c => c.FunnyID == id), new Func<Comment, DateTime>(c => c.Time)); } if (Request.IsAjaxRequest()) { ViewBag.FunnyID = id; return PartialView("~/Views/Comment/FunnyComment.cshtml",list); } Funny funny = iFunny.GetDetail(id); ViewData["list"] = list; return View(funny); }
怎么会出现这么多相同的请求,请求的内容均相同,怎么去避免啊??
# | Result | Protocol | Host | URL | Body | Caching | Content-Type | Process | Comments | Custom |
---|---|---|---|---|---|---|---|---|---|---|
2 | 200 | HTTP | localhost:4746 | /Funny/Details/1/1?X-Requested-With=XMLHttpRequest | 1,215 | private | text/html; charset=utf-8 | chrome:1456 | ||
3 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752157582 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
4 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752157682 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
5 | 200 | HTTP | localhost:4746 | /Funny/Details/1/2?X-Requested-With=XMLHttpRequest | 1,218 | private | text/html; charset=utf-8 | chrome:1456 | ||
6 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752165150 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
7 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752166367 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
8 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752166675 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
9 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752166775 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
12 | 200 | HTTP | localhost:4746 | /Funny/Details/1/3?X-Requested-With=XMLHttpRequest | 1,140 | private | text/html; charset=utf-8 | chrome:1456 | ||
13 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169468 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
14 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169540 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
15 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169601 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
16 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169660 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
17 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169731 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
18 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169808 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
19 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169865 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
20 | 200 | HTTP | localhost:4746 | /WebResource.axd?d=bshe226JCqVVz7L5K_rkjyylqKpko_MoMW5NoahySLQsNdkh2uXwSdPfkqkfx_gx9bqP30X6utr8HALfguaNCzE-uCz9eX7iDUc4P1sF50FfW4iHYDjE6tHGo0bErM_TZfvRefAtzlVW9ynTbiAIfw2&t=635149438820000000&_=1404752169920 | 2,611 | public; Expires: Tue, 07 Jul 2015 16:49:08 GMT | text/javascript | chrome:1456 | ||
21 | 200 | HTTP | sub.adtchrome.com | /videoadjs.txt?addonName=%E5%B9%BF%E5%91%8A%E7%BB%88%E7%BB%93%E8%80%85&addonVersion=3.1.1&application=chrome&applicationVersion=35.0.1916.153&platform=chromium&platformVersion=35.0.1916.153&lastVersion=0&t=1404752179383 | 2,486 | public, max-age=31536000 | text/plain | chrome:1456 |
返回的js:
(function(n){function f(n){this.wrapper=n}function o(n,t){if(t!==null&&t!==undefined)for(var i=0;i<t.length;i++)n.push({name:t[i].name,value:t[i].value})}function u(t){var f=window.location.hash.substring(1),r,i,u;if(n.trim(f)!="")for(r=f.split("&"),i=0;i<r.length;i++)if(u=r[i].split("="),u[0].toString().toLowerCase()===t.toString().toLowerCase())return parseInt(u[1])||1;return 0}function t(t,i){var r=window.location.hash.substring(1),u,f;n.trim(r)==""?window.location.hash=t+"="+i:(u=new RegExp(t+"=[^&]*","i"),u.test(r)?(f=r.replace(u,t+"="+i),window.location.hash=f):window.location.hash+="&"+t+"="+i)}function i(t,i){for(var r=window,u=(t||"").split(".");r&&u.length;)r=r[u.shift()];if(typeof r=="function")return r;if(n.trim(t).toLowerCase().indexOf("function")==0)return new Function("return ("+t+").apply(this,arguments);");i.push(t);try{return Function.constructor.apply(null,i)}catch(f){alert("Error:\r\n"+t+"\r\nis not a valid callback function")}}function e(n){var t,i,r;window.event?(t=n.keyCode,i=n.srcElement):n.which&&(t=n.which,i=n.target),r=[8,37,39,46,48,49,50,51,52,53,54,55,56,57,96,97,98,99,100,101,102,103,104,105],t!==null&&r.indexOf(t)<0&&(t==13&&i!=null&&i.click(),n.preventDefault?n.preventDefault():event.returnValue=!1)}function r(i){var r=n(i).closest("[data-mvcpager=true]"),h=r.data("maxpages"),c=r.data("invalidpageerrmsg"),l=r.data("outrangeerrmsg"),f=r.data("firstpage"),a=r.data("urlformat"),e=r.data("pageparameter"),v=r.data("ajax"),u=0,o=r.find("select[data-pageindexbox=true],input:text[data-pageindexbox=true]"),s;if(o.length>0&&(u=o.val()),s=new RegExp("^\\s*(\\d+)\\s*$"),s.test(u)){if(RegExp.$1<1||RegExp.$1>h){alert(l);return}}else{alert(c);return}v?t(e,u):self.location.href=typeof f!="undefined"&&f!==!1&&u=="1"?f:decodeURI(a).replace("__"+e+"__",u)}n.fn.initMvcPagers=function(){return this.each(function(){new f(this).init()})},f.prototype={wrapper:null,url:null,pageIndexName:null,updateTarget:null,onBegin:null,onComplete:null,onFailure:null,onSuccess:null,httpMethod:null,confirm:null,loadingElementId:null,loadingDuration:0,partialLoading:null,currentPage:null,dataFormId:null,isFirstLoad:!0,initIndex:null,allowReload:!1,searchCriteria:null,init:function(){var i=n(this.wrapper),l=i.data("ajax")||!1,h=i.data("pageparameter"),o,s,f,c;if(this.pageIndexName=h,l){this.updateTarget=i.data("ajax-update"),this.onBegin=i.data("ajax-begin"),this.onComplete=i.data("ajax-complete"),this.onFailure=i.data("ajax-failure"),this.onSuccess=i.data("ajax-success"),this.confirm=i.data("ajax-confirm")||undefined,this.httpMethod=i.data("ajax-method")||"GET",this.loadingElementId=i.data("ajax-loading")||undefined,this.dataFormId=i.data("ajax-dataformid")||undefined,this.loadingDuration=i.data("ajax-loading-duration")||0,this.partialLoading=i.data("ajax-partialloading")||!1,this.currentPage=i.data("ajax-currentpage")||1,this.url=i.data("urlformat"),this.initIndex=this.currentPage,o="[data-mvcpager=true]",s=u(h),s!=this.currentPage&&s>0&&this.loadData(s,{type:this.httpMethod,data:[]}),this.dataFormId!==undefined&&(f=this,c=n(f.dataFormId).data("ajax")||!1,n(f.dataFormId).submit(function(i){f.searchCriteria=n(f.dataFormId).serializeArray(),c?f.currentPage!==1?(f.currentPage=1,t(f.pageIndexName,-1)):f.allowReload=!0:(f.allowReload=!0,f.currentPage===1?f.loadData(1,{type:f.httpMethod,data:[]}):(t(f.pageIndexName,1),f.currentPage=1),i.preventDefault())})),this.initHashChange();n(this.updateTarget).on("click",o+" a[data-pageindex]",function(i){var r=n(this).data("pageindex");i.preventDefault(),t(h,r)});n(this.updateTarget).on("keydown",o+" input:text",function(){e(event)});n(this.updateTarget).on("click",o+" input[type=button][data-submitbutton=true]",function(){r(this)});n(this.updateTarget).on("change",o+" select[data-autosubmit=true],input:text[data-autosubmit=true]",function(){r(this)})}else jQuery("input:text",this.wrapper).keydown(function(n){e(n)}),jQuery("input[type=button][data-submitbutton=true]",this.wrapper).click(function(){r(i)}),jQuery("select[data-autosubmit=true],input:text[data-autosubmit=true]",this.wrapper).change(function(){r(i)})},initHashChange:function(){var r=document.documentMode,t=this,i;"onhashchange"in window&&(r===undefined||r>7)?n(window).bind("hashchange",function(){var n=u(t.pageIndexName);n===0&&(n=t.initIndex),t.loadData(n,{type:t.httpMethod,data:[]})}):(i=window.location.hash,setInterval(function(){if(window.location.hash!=i){i=window.location.hash;var n=u(t.pageIndexName);n===0&&(n=t.initIndex),t.loadData(n,{type:t.httpMethod,data:[]})}},200))},loadData:function(t,r){var u=this,f;t!==-1&&(t!==0||!u.isFirstLoad)&&(t!=u.currentPage||u.allowReload)&&(!u.confirm||window.confirm(u.confirm))&&(n.extend(r,{url:this.url.replace("__"+u.pageIndexName+"__",t),beforeSend:function(t){var f=r.type.toUpperCase(),e;return f==="GET"||f==="POST"||t.setRequestHeader("X-HTTP-Method-Override",f),e=i(u.onBegin,["xhr"]).apply(this,arguments),e!==!1&&u.loadingElementId!==undefined&&n(u.loadingElementId).show(u.loadingDuration),e},complete:function(){u.loadingElementId!==undefined&&n(u.loadingElementId).hide(u.loadingDuration),i(u.onComplete,["xhr","status"]).apply(this,arguments)},success:function(r){u.partialLoading?n(u.updateTarget).html(n(u.updateTarget,r).html()):n(u.updateTarget).html(r),u.currentPage=t,u.isFirstLoad=!1,i(u.onSuccess,["data","status","xhr"]).apply(this,arguments)},error:i(u.onFailure,["xhr","status","error"])}),u.dataFormId!==undefined&&o(r.data,u.searchCriteria),r.data.push({name:"X-Requested-With",value:"XMLHttpRequest"}),f=r.type.toUpperCase(),f==="GET"||f==="POST"||(r.type="POST",r.data.push({name:"X-HTTP-Method-Override",value:f})),n.ajax(r))}}})(jQuery),$(function(){$("[data-mvcpager=true]").initMvcPagers()});
@{Html.RegisterMvcPagerScriptResource();
应放在父视图中,而不是放在子视图中
自己写一个分页很复杂吗?
用的是Html.AjaxPager?我用firebug测试了一下,一次只发一个请求呀,没出现你说的多个请求的情况。
我估计问题可能不在这里,你的JS里是不是有什么刷新页面的操作之类的东西?
我的脚本放错地方了,放在部分视图内,每次都刷新。