通过ajax发送请求时自动渲染部分
我试图弄清楚当通过 ajax 发出请求时如何自动将视图呈现为部分视图(无母版页)。
我想避免的是在每个可以返回 ajax 的控制器方法中使用以下代码(因为这不是很干燥):
return Request.IsAjaxRequest() ? PartialView(model) : View(model)
我的第一个想法是在 View
方法中将检查添加到我的基本控制器中。但视图方法返回一个View
(PartialView
不继承)。所以失败了。
我的下一个尝试是尝试在我的自定义剃刀视图引擎中进行检查,并简单地删除母版页(如果它是 ajax 请求)。但这也失败了。
我能做的是创建一个新方法 ViewOrPartial
,其中包含检查并相应地返回结果。
你会怎么做呢?
I'm trying to figure out how I can automatically render a view as a partial (no master page) when the request is made through ajax.
What I want to avoid is to use the following code in every controller method that can return ajax (since that's not very DRY):
return Request.IsAjaxRequest() ? PartialView(model) : View(model)
My first thought was to add the check into my base controller in the View
method. But the view method returns a View
(which PartialView
do not inherit). So that failed.
My next attempt was to try to make the check in my custom razor view engine and simple remove the master page if it's a ajax request. But that failed too.
What I can do is to create a new method ViewOrPartial
which contains the check and return a result accordingly.
How would you have done it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如何将以下内容放入
~/Views/_ViewStart.cshtml
中并在控制器操作中仅返回一个视图:所有 razor 视图都使用
_ViewStart.cshtml
。这是指定母版页的地方。因此,通过进行此更改,仅当请求不是 AJAX 请求时,所有视图才会有条件地自动应用母版页。相当干。How about putting the following:
in your
~/Views/_ViewStart.cshtml
and in your controller action simply returning a view:All razor views use
_ViewStart.cshtml
. This is where the master page is specified. So by making this change all views automatically will apply the master page only conditionally if the request was not an AJAX request. Pretty DRY.我在这里概述了如何执行此操作:
简单的 ASP.NET MVC CRUD 视图在 JavaScript UI 对话框中打开/关闭
I outline how to do this here:
Simple ASP.NET MVC CRUD views opening/closing in JavaScript UI dialog
另一种方法是通过 jQuery 加载它: 使用 jQuery ajax 在 ASP.NET MVC 中加载部分视图,然后再次检索服务器上的输入。
An alternative could be to load it via jQuery: Using jQuery ajax to load a partial view in ASP.NET MVC and then retrieve the input on the server again.
我在实践中发现
IsAjaxRequest
并不是 100% 可靠。随着来自移动设备的大量请求,它偶尔会在 Ajax 调用中返回
false
。我最终向我的 Ajax 请求添加了一个查询字符串参数(例如,partial=1),以确保控制器不会被愚弄。这是 MVC 4 左右的问题,因此从那时起可能已得到纠正,但我不能冒险。
在我的基本控制器(由我的所有控制器继承)中,我添加了以下内容:
在控制器中,我有类似的内容:
最后在我的视图中,我有:
我确保我始终将
&partial=1
添加到我的 Ajax 请求(以防万一)从那时起它就 100% 可靠。I found in practice that
IsAjaxRequest
is not 100% reliable.With loads of requests from a mobile device it would occasionally return
false
on an Ajax call.I wound up adding a query string parameter (e.g. partial=1) to my Ajax requests to ensure the controller was not fooled. This was around MVC 4 so may have been corrected since then, but I can't afford to take a chance.
In my base controller (inherited by all my controllers) I added this:
In the controllers I then have something like:
And finally in my views I have:
I ensure I always add
&partial=1
to my Ajax requests (just in case) and it has been 100% reliable since.