简体   繁体   中英

ASP.net Razor Refresh on partial view returns to home page

I am using ajax to call controllers to render partial views. Everytime I hit the refresh button or back button it returns to my homepage/index instead of to the partial view page that was being displayed. Anyone know a solution?

There are a few ways to handle this, one is have your partial view add a hash or querystring to the url, and have handling in your JavaScript or controller to know how to grab the hash/querystring and load the right partial view on the page reload.

But if you need to keep track of what they were doing before hitting reload, you could either use something that is holding their location server-side, or caching client-side. For tips on caching, look at this or this .

I found both work equally well, but going with the hash or querystring will show in the url, not always what you are going for.

I'm not sure what frameworks you are using on the client-side (knockout.js, jQuery, etc.) but a lot of these make this stuff easier, you could create a simple cache of your views on load, adding the needed views to the cache based on whatever order you need them, and just inject it into a div or something when they call for it.

The issue here, is that partial views are like code snippits in a way, the server knows how to dish them up, and the client knows how to view them, but the browser doesn't view them as entries into your browser history so you have to tell the browser how to ensure your data gets reloaded how you want.

Another option you could look at would be injecting them into the browser history, but you still need a way for the server to dish up the right partial on load. I found this here on a guy's implementation of loading partials for single-page web applications, seemed like a good option. But again, you need to know how you are going to dish up that data on a reload. But to answer the comment, there isn't some magical way for the browser to know what you are calling with ajax. if you want the browser to know about it automatically, try using @Html.Partial(string partialViewName) or @Html.RenderPartial(string partialViewName) see MSDN on these .

You could also look into putting something in the viewdata, tempdata, viewbag, or session data look at this article on the differences, to see if one would work for your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM