简体   繁体   English

什么时候适合使用Html.RenderAction()?

[英]When Is It Appropriate to Use Html.RenderAction()?

I'm a little bit unsure about when it's appropriate to use Html.RenderAction() to render my Views and when not to. 我有点不确定何时使用Html.RenderAction()来渲染我的视图以及何时不使用它。 My understanding is that because it's not an 'official' component of ASP.NET MVC it's bad practice to use it, and its original intention was for reusable widgets that don't exist in any specific Controller context. 我的理解是,因为它不是ASP.NET MVC的“官方”组件,所以使用它是不好的做法,它的初衷是用于任何特定Controller上下文中都不存在的可重用小部件。

The thing is, RenderAction is very useful when I need a component that exists under a different Controller than the one that I'm currently rendering the View for. 问题是,当我需要一个存在于不同Controller下的组件时,RenderAction非常有用,而不是我当前渲染View的那个。 I think it's a very tidy & self contained way to render components that rely on data unavailable in the current View. 我认为这是一种非常整洁和自包含的方式来呈现依赖于当前视图中不可用的数据的组件。 My View doesn't need to supply the Model, as it would if I was using RenderPartial() My View不需要提供Model,就像我使用RenderPartial()

Is this bad practice? 这是不好的做法吗? Is there a better way? 有没有更好的办法?

它可以解决你的问题。

I think it's a very tidy & self contained way to render components that rely on data unavailable in the current View. 我认为这是一种非常整洁和自包含的方式来呈现依赖于当前视图中不可用的数据的组件。 My View doesn't need to supply the Model, as it would if I was using RenderPartial() My View不需要提供Model,就像我使用RenderPartial()一样

It actually is. 它实际上是。 For instance, you could create some small view to serve as widgets and inject them wherever you need. 例如,您可以创建一些小视图作为小部件,并将它们注入到您需要的任何位置。 Processing user input from those widgets however may get even complicated, but that's another issue. 然而,从这些小部件处理用户输入可能甚至变得复杂,但这是另一个问题。

One other legitimate scenario I can think of is using HTML email templates. 我能想到的另一个合法场景是使用HTML电子邮件模板。 It's one case when you clearly don't need to return the rendered output directly to the browser but rather insert it into email body. 这是一种情况,当您显然不需要将呈现的输出直接返回到浏览器,而是将其插入到电子邮件正文中。

I use Html.RenderAction() for the reasons you give, so that you don't have to supply the same data over and over again to every single view that needs to display user information (for example). 我使用Html.RenderAction()是出于你给出的原因,这样你就不必一遍又一遍地向每个需要显示用户信息的视图提供相同的数据(例如)。 You can argue that it is violating the mvc pattern as the view now knows about the controller. 您可以认为它违反了mvc模式,因为视图现在知道控制器。 But I think that the advantages in this scenario outweighs that and your application will be more DRY. 但我认为这种情况下的优势超过了这一点,您的应用程序将更加干燥。

I simply use it for everything that I need to reuse in many different places (for example user data that I display on every page from my master page) and I don't want to send that information explicitly to each view. 我只是将它用于我需要在许多不同的地方重用的所有内容(例如我在主页的每个页面上显示的用户数据),我不想将这些信息显式发送到每个视图。 If I'm not mistaken I think that they have included it in asp.net mvc 2 as well, so It's now part of the framework. 如果我没弄错的话,我认为他们也将它包含在asp.net mvc 2中,所以它现在已成为框架的一部分。

I have found a great reason to use Html.RenderAction in a scenario based upon results from the parent view's model. 我发现一个很好的理由在基于父视图模型的结果的场景中使用Html.RenderAction。 For example, the parent view's model had a List property that had to show in a table. 例如,父视图的模型具有必须在表中显示的List属性。 However, to prevent conditional IF/ELSE in the parent view, I call Html.RenderAction(). 但是,为了防止父视图中的条件IF / ELSE,我调用了Html.RenderAction()。 The action takes in the List and checks the count. 该操作接受List并检查计数。 If the count is zero, it returns a 'no results' view; 如果计数为零,则返回“无结果”视图; otherwise, it returns a view that processes the items in the List in the form of its own model. 否则,它返回一个视图,以自己的模型的形式处理List中的项目。 This is cleaner by preventing logic being inserted in the view. 通过防止在视图中插入逻辑,这是更清晰的。 I can also reuse the "no results" view for other areas in my app. 我还可以在我的应用中为其他区域重复使用“无结果”视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM