通过ajax发送请求时自动渲染部分

发布于 2024-10-04 16:48:45 字数 459 浏览 3 评论 0原文

我试图弄清楚当通过 ajax 发出请求时如何自动将视图呈现为部分视图(无母版页)。

我想避免的是在每个可以返回 ajax 的控制器方法中使用以下代码(因为这不是很干燥):

return Request.IsAjaxRequest() ? PartialView(model) : View(model)

我的第一个想法是在 View 方法中将检查添加到我的基本控制器中。但视图方法返回一个ViewPartialView 不继承)。所以失败了。

我的下一个尝试是尝试在我的自定义剃刀视图引擎中进行检查,并简单地删除母版页(如果它是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相思碎 2024-10-11 16:48:45

如何将以下内容放入

@{
    Layout = !Request.IsAjaxRequest() ? "~/Views/Shared/_Layout.cshtml" : null;
}

~/Views/_ViewStart.cshtml 中并在控制器操作中仅返回一个视图:

return View(model);

所有 razor 视图都使用 _ViewStart.cshtml。这是指定母版页的地方。因此,通过进行此更改,仅当请求不是 AJAX 请求时,所有视图才会有条件地自动应用母版页。相当干。

How about putting the following:

@{
    Layout = !Request.IsAjaxRequest() ? "~/Views/Shared/_Layout.cshtml" : null;
}

in your ~/Views/_ViewStart.cshtml and in your controller action simply returning a view:

return View(model);

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.

莫相离 2024-10-11 16:48:45

我在实践中发现 IsAjaxRequest 并不是 100% 可靠。

随着来自移动设备的大量请求,它偶尔会在 Ajax 调用中返回 false

我最终向我的 Ajax 请求添加了一个查询字符串参数(例如,partial=1),以确保控制器不会被愚弄。这是 MVC 4 左右的问题,因此从那时起可能已得到纠正,但我不能冒险。

在我的基本控制器(由我的所有控制器继承)中,我添加了以下内容:

public bool IsPartialPageRequest
{
    get
    {
        return Request.IsAjaxRequest() || !string.IsNullOrEmpty(Request["partial"]);
    }
}

在控制器中,我有类似的内容:

    if (!base.IsPartialPageRequest)
    {
        ViewBag.Layout = "~/Views/Shared/_Layout.cshtml";
    }

最后在我的视图中,我有:

@{
    Layout = ViewBag.Layout;
}

我确保我始终将 &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:

public bool IsPartialPageRequest
{
    get
    {
        return Request.IsAjaxRequest() || !string.IsNullOrEmpty(Request["partial"]);
    }
}

In the controllers I then have something like:

    if (!base.IsPartialPageRequest)
    {
        ViewBag.Layout = "~/Views/Shared/_Layout.cshtml";
    }

And finally in my views I have:

@{
    Layout = ViewBag.Layout;
}

I ensure I always add &partial=1 to my Ajax requests (just in case) and it has been 100% reliable since.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文