.NET MVC2 Master 上的多个部分视图,也可通过 AJAX 单独加载
我正在使用 MVC2 创建一个单页网站,因此一些不同的页面将像 HTML div 中一样可见:
==========
|Section1|
| |
==========
==========
|Section2|
| |
==========
==========
|Section3|
| |
==========
让我们将此登陆页面称为 /Home/Index - 在 Home/Index 视图中,只有几个 RenderAction 调用来渲染第 1/索引、第 2/索引和第 3/索引 - 每个部分都是一个控制器。
在不同的点,Section1、Section2 或Section3 的不同部分将通过向/Section1/Part1 或/Section2/Part3 等发出Ajax 请求来加载到各自的div 中。
我已经知道获得页面的最佳方式第一次加载将是为每个部分提供部分视图,然后将它们放入。
但是假设我想在不使用 AJAX 请求的情况下访问 /Section2/Part3,那么最好的方法是什么确保我在页面上显示第 1 部分、第 2 部分和第 3 部分,但我们显示的是第 3 部分,而不是第 2 部分的默认部分视图。
任何想法将不胜感激:)
我的一个想法是:以某种方式从部分操作返回一个 /Home/Index 视图,其中包含渲染指定部分所需的模型数据 - 但我不想重定向,因为 URL 需要保持不变。
希望这是有道理的。谢谢:)
PS 不使用 .NET Ajax。
I'm creating a single-page site using MVC2 and thus a few different pages will be visible like so in HTML divs:
==========
|Section1|
| |
==========
==========
|Section2|
| |
==========
==========
|Section3|
| |
==========
Let's call this landing page /Home/Index - in the Home/Index view there's just a few RenderAction calls to render Section1/Index, Section2/Index and Section3/Index - each of the sections is a controller.
At various points, a different part of Section1, Section2 or Section3 will be loaded into their respective divs by making an Ajax Request to /Section1/Part1 or /Section2/Part3 etc, etc.
I know already that the best way to have the page loaded the first time would be to have partial views for each of these sections, and putting those in.
But say I wanted to go to /Section2/Part3 without using an AJAX request, what would be the best way to ensure that I get Section1, Section2 and Section3 on the page showing but, instead of the default Partial View for Section2, we show Part3.
Any ideas would be appreciated :)
One I had was: somehow returning from the Section actions a /Home/Index view with the Model data required to know to render the specified part - but I don't want to redirect as the URL needs to stay the same.
Hope this makes sense. Thanks :)
P.S. Not using .NET Ajax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的问题可以通过在操作方法中使用
Request.IsAjax
来解决。如果您有 AJAX 请求,请返回部分视图。如果没有,则返回包含部分视图的常规视图。在
Part1Full
视图中,只需调用RenderPartial("Part1")
。我假设如果您调用RenderAction("Part1")
但如果不查看[ChildActionOnly]
属性,它也会起作用。您可以在此处找到更多信息。I think your issue may be solved by using
Request.IsAjax
in your action methods. If you have an AJAX request, return the partial view. If not, return a regular view that includes the partial.In your
Part1Full
view, just callRenderPartial("Part1")
. I'm assuming it will also work if you callRenderAction("Part1")
but if not look into the[ChildActionOnly]
attribute. You can find more information on that here.