从控制器渲染局部视图 [英] Render partial view from controller

查看:104
本文介绍了从控制器渲染局部视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器ItemsController,该控制器的实例方法GetRecent()返回IQueryable<Item>

I have a controller ItemsController that has an instance method GetRecent() which returns IQueryable<Item>

在我的应用程序的索引页面中,我想使用ItemsController.GetRecent()

In the index page of my application I want to render a partial view _Recent.cshtml using the results from ItemsController.GetRecent()

为此,我在Index.cshtml中写了以下内容

To do this, I've written the following in my Index.cshtml

我已经将名称空间引用添加到〜/Views/web.config中,因此我不需要写出控制器的完整名称空间

I've added namespace references to the ~/Views/web.config so I don't need to write out the controllers full namespace

    @{
        ViewBag.ItemsController = new ItemsController();
    }

    ...

    @Html.Partial("~/Views/Items/_Recent.cshtml",
                   ((ItemsController)ViewBag.ItemsController).GetRecentItems())

我考虑过直接传递new ItemsController().GetRecentItems()或将GetRecentItems()转换为静态方法,但是我不确定这应该朝哪个方向前进.

I thought of passing new ItemsController().GetRecentItems() directly or turning GetRecentItems() into a static method however I'm not sure what direction this should take.

我想知道这是否是从控制器构建局部视图的一种公认方法,如果不是,那么如何才能更有效地完成它?

I want to know if this is an accepted way of building a partial view from a controller and if not, how can this be accomplished more efficiently?

推荐答案

RenderAction

第一个选项是Html.ActionHtml.RenderAction ( ASP.Net MVC框架的一部分,并提供更完善的方法来实现类似于您的代码示例的内容.

RenderAction

The first option is Html.Action or Html.RenderAction(What's the difference?). These are part of the ASP.Net MVC framework, and provide a more polished means to implement something similar to your code sample.

使用这种方法,您的视图将调用适当的控制器,该控制器将获取所有需要的数据,呈现部分视图,然后将其返回.

With this approach, your view calls the appropriate controller, which fetches any needed data, renders the partial view, and returns it.

我倾向于回避使用这种方法,因为这种方法看起来很落后,即视图知道"了很多关于控制器和控制器结构的信息,并且正在积极地执行而不是仅仅消耗.

I tend to shy away from using this approach as it seems backwards, i.e. the view "knows" quite a bit about controllers and controller structure and is actively executing rather than just consuming.

仍然,这是一个合法的选择.

Still, it's a legitimate option.

您可以将最近项"与主视图的视图模型一起传递.这需要更多的建模工作,但是纯粹"的原因在于您的视图与控制器的耦合较少.您的局部视图无需更改.

You could pass the "recent items" along with the view model for the main view. This is more effort to model, but "pure" in that your view is less coupled to the controllers. Your partial view would not need to change.

您可以使用AJAX调用适当的控制器来渲染最近的项目,并使该控制器返回部分视图作为其结果.这可能是最费力的工作,但是却很优雅,因为某些工作会延迟到页面的其余部分呈现出来为止(可能允许延迟加载和/或缩短页面加载时间).

You could render the recent items using an AJAX call to the appropriate controller, and have that controller return the partial view as its result. This is probably the most effort, but elegant in that some of the work is delayed until the rest of the page is rendered (possibly allowing lazy loading, and/or improved page load times).

做得正确,这种方法还可以将关注点分离开来.

Done correctly, this approach also allows decent separation of concerns.

  • 相关问题(不完全相同,因为它不关注将数据传递到局部视图)
  • Related Question (not identical, as it doesn't focus on passing data to the partial view)

这篇关于从控制器渲染局部视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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