简体   繁体   English

部分视图上的ASP.net Razor刷新返回首页

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

I am using ajax to call controllers to render partial views. 我正在使用ajax来调用控制器以呈现部分视图。 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. 有几种处理方法,一种是让部分视图向URL添加哈希或查询字符串,并在JavaScript或控制器中进行处理以了解如何获取哈希/查询字符串并在页面上加载正确的部分视图重装。

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 . 有关缓存的提示,请查看thisthis

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. 我发现两者都能很好地工作,但是使用哈希或查询字符串将显示在url中,而不总是显示您要使用的内容。

EDIT 编辑

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. 我不确定您在客户端使用的是什么框架(knockout.js,jQuery等),但是其中许多框架使此操作变得更容易,您可以创建一个加载时视图的简单缓存,添加所需的内容根据您需要的顺序访问缓存视图,并在需要时将其注入div或其他内容。

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. 我在一个家伙为单页Web应用程序加载部分的实现中发现了这一点 ,这似乎是一个不错的选择。 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. 但是要回答评论,浏览器并没有一种神奇的方式可以知道您使用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 . 如果您希望浏览器自动了解它,请尝试使用@Html.Partial(string partialViewName) or @Html.RenderPartial(string partialViewName)参见MSDN

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. 您还可以考虑在视图数据,tempdata,viewbag或会话数据中放置一些内容 ,以查看本文中的区别,以了解是否可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ASP.NET MVC和jQuery呈现部分视图/提交表单,而无需刷新页面 - Using ASP.NET MVC and jQuery to render partial view/submit a form without page refresh 如何在ASP.NET MVC中刷新页面后保留注入的局部视图 - How to retain an injected partial view after page refresh in asp.net mvc 仅刷新 ASP.NET CORE Razor 页面中的内容页面 - Refresh only content page in ASP.NET CORE Razor Pages 没有UpdatePanel的asp.net中的自动部分页面刷新 - auto partial page refresh in asp.net without UpdatePanel ASP.NET MVC 3 Razor部分视图-主布局中包含jQuery - ASP.NET MVC 3 Razor Partial View - jQuery Included In Main Layout ASP Net MVC 5在部分视图中刷新表 - Asp Net MVC 5 refresh a table in a partial view 如何在我的 ASP.NET Core Razor 视图中更新/刷新 ViewData? - How can I update/refresh ViewData in my ASP.NET Core Razor View? 在 asp.net 内核中使用 javascript/jquery 刷新 razor 页面而不重新加载 - Refresh razor page without reload using javascript/ jquery in asp.net core 一个页面上的Asp.net MVC多个(相同)局部视图<script> tags inside Partial View - Asp.net MVC Multiple (of the same) Partial Views on a page with <script> tags inside Partial View ASP.net 核心 Razor 使用 AJAX 上传页面文件到 IFormFile 处理程序返回 404 Not Found - ASP.net core Razor page file upload with AJAX Post to IFormFile handler returns 404 Not Found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM