MVC 3:通过AJAX加载时如何渲染视图没有它的页面布局? [英] MVC 3: How to render a view without its layout page when loaded via ajax?

查看:207
本文介绍了MVC 3:通过AJAX加载时如何渲染视图没有它的页面布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关逐步增强和我有一个关于AJAXifying的看法问题。在我的MVC 3的项目我有一个布局页面,一个页面viewstart和两个普通的看法。

I am learning about Progressive Enhancement and I have a question about AJAXifying views. In my MVC 3 project I have a layout page, a viewstart page, and two plain views.

该viewstart页面在浏览文件夹的根,因此适用于所有的意见。它规定,所有的意见应该使用 _Layout.cshtml 它们的布局页。布局页面包含两个导航链接,每个图。该链路使用 @ Html.ActionLink()来渲染自己的网页。

The viewstart page is in the root of the Views folder and thus applies to all views. It specifies that all views should use _Layout.cshtml for their layout page. The layout page contains two navigation links, one for each view. The links use @Html.ActionLink() to render themselves to the page.

现在我已经加入jQuery和要劫持这些链接,并使用Ajax来加载自己的动态网页内容。

Now I have added jQuery and want to hijack these links and use Ajax to load their content on the page dynamically.

<script type="text/javascript">
    $(function () {
        $('#theLink').click(function () {
            $.ajax({
                url: $(this).attr('href'),
                type: "GET",
                success: function (response) {
                    $('#mainContent').html(response);
                }
            });
            return false;
        });
    });
</script>

有两种方式我能想到的要做到这一点,但我特别不喜欢任何一个:

There are two ways I can think of to do this, but I don't particularly like either one:

1)我可以把整个视图中的内容,并将其放置在一个局部视图,然后让主视图调用局部视图时,它的呈现方式。这样一来,使用 Request.IsAjaxRequest()在控制器中,我可以返回查看()或返回 PartialView()基于请求是否是一个Ajax请求。我不能常规视图返回Ajax请求,因为那就使用的布局页,我会得到注入布局页面的第二个副本。但是,我不喜欢这个,因为它迫使我创建只是一个 @ {Html.RenderPartial();}空的观点。在其中为标准的GET请求

1) I can take the entire View's contents and place them in a partial view, then have the main view call the partial view when it is rendered. That way, using Request.IsAjaxRequest() in the controller, I can return View() or return PartialView() based on whether or not the request is an Ajax request. I can't return the regular view to the Ajax request because then it would use the layout page and I'd get a second copy of the layout page injected. However, I don't like this because it forces me to create empty views with just a @{Html.RenderPartial();} in them for the standard GET requests.

    public ActionResult Index()
    {
        if (Request.IsAjaxRequest())
            return PartialView("partialView");
        else
            return View();
    }

然后在Index.cshtml做到这一点:

Then in Index.cshtml do this:

@{Html.RenderPartial("partialView");}

2)我可以删除_viewstart布局指定和手动指定它时,请求未阿贾克斯:

2) I can remove the layout designation from _viewstart and specify it manually when the request is NOT Ajax:

    public ActionResult Index()
    {
        if (Request.IsAjaxRequest())
            return View(); // Return view with no master.
        else
            return View("Index", "_Layout"); // Return view with master.
    }

有没有人有更好的建议?有没有一种方法返回一个观点没有它的页面布局?这将是更容易明确地说不包括你的布局,如果它是一个Ajax请求,比这将是明确包括的布局,如果它不是一个ajax。

Does anyone have a better suggestion? Is there a way to return a view without its layout page? It would be much easier to explicitly say "don't include your layout" if it is an ajax request, than it would be to explicitly include the layout if it's not an ajax.

推荐答案

〜/查看/ ViewStart.cshtml

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

和在控制器:

public ActionResult Index()
{
    return View();
}

这篇关于MVC 3:通过AJAX加载时如何渲染视图没有它的页面布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆