简体   繁体   中英

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. 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.

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. 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()

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()

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. 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). You can argue that it is violating the mvc pattern as the view now knows about the controller. 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.

I have found a great reason to use Html.RenderAction in a scenario based upon results from the parent view's model. For example, the parent view's model had a List property that had to show in a table. However, to prevent conditional IF/ELSE in the parent view, I call Html.RenderAction(). The action takes in the List and checks the count. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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