13

I still don't get the primary purpose of Html.Action in asp.net mvc. I have been using Html.Partial every time I need to load up a partial view or wanted to split up some code in a view to clean it up.

Where does Html.Action fit into all of this (e.g. where would you want to use Html.Action and not use Html.Partial)?

Edit

The answers seem to be use Html.Action for dynamic data. I don't get this as you can use Partial Views for Dynamic data as well.

For instance if a user on my site edits a row. A ajax call is made to a method and I go grab that row from the db. I then return a parital view of a form that has all the data on it ready for editing. If an error occurs then I return a json result with the error method and my javascript consumes it and alerts the user.

If all is good then the rendered html is put into a jquery dialog and displayed to the user.

Is it because you can use the "ChildActionOnlyAttribute" that makes people use Action instead?

chobo2
  • 83,322
  • 195
  • 530
  • 832

6 Answers6

12

Ankur has the right idea but I find you can really simplify the concept down further.

For me it comes down to What versus How

If you know what you want to render but not how it's likely you'll use a partial to let it determine how to render the information. For example, maybe your view model is for an invoice. Your invoice view model probably already has all the information you need about the invoice itself, including an enumerable of the line items on the invoice perhaps. A partial might be a good choice for the line items so that it's self contained. You already have the line items details (the what), but a partial will handle how it gets rendered (the how)

On the flip side, maybe your invoice view model has a customer ID on it but no actual customer details. Here you don't have the what, so you'd pass in the customer ID to an Action and it'll get what data it needs and pass it off to the view to render how it seems fit.

So in summary if you already have all the data you want to work with, just stick with a Partial, but if you are missing information that you need to obtain, Action would be better.

Where this get really fuzzy around the edges is when a Partial view includes the ability to retrieve it's own data via Ajax (or other technologies). In which case you might be able to get away with making that Customer details portion in my example, a Partial, and have it retrieve the data it needs Using Ajax after the client get's the response. But that's more up to you if that sort of thing even makes sense for your implementation.

Addendum: It's worth noting that if you decide to try out ASP.NET MVC Core, ChildActions are no longer available. In which case your choices will be limited to partial views, ajax, or the newly introduced feature of Components. The last of which is similar to ChildActions, but slightly different in how they are implemented.

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • This is probably why I am not use to it all. I grab most partial views with ajax. I thought I understood that action could be away to get around not having to have viewmodel inside a view model but it seems that in this case you should still use partial views. http://pratapreddypilaka.blogspot.ca/2011/11/htmlpartial-vs-htmlaction-mvc-razor.html It seems like the best time to use action is when you would need the same model multiple times – chobo2 Sep 25 '12 at 22:34
  • Because of technologies like Ajax, they are both plausible options. But keep in mind child actions are server side, while partials are client side (in the way that they access data). In some cases you may not want to expose a way to access certain data via Ajax, or likewise, you may want to use an Action to give the flexibility of being embedded in a parent page or as a standalone page (if the child attribute only is omitted). Also depending on the client, JavaScript might not be available for Ajax, though this argument isn't that realistic with today's selection of browsers. – Nick Albrecht Sep 26 '12 at 16:04
2

Perhaps an example will make this clearer.

Let's say you have a menu which appears on every page, so you put it in your layout. The menu will never change - there is just some basic navigation links, Home, About, Contact us etc, so you just use a normal partial view. This will work fine - as the content is static - you don't need to go to a database to get the data. You can just use @Html.Partial("Menu");.

Later you decide you need to change the menu so that it gets all the links from a database. You update your partial view to have a model that is a List<string> - one for each link.

Now, if you still want to just use a Partial View, every action would need to query the database to get the list of links, and every Model for every View would need to have a List<string> property for the links, so that it could pass this to the Menu Partial View. This would be a bad idea.

Instead, you would make a new Child Action GetMenuLinks() - this would query the database to get the links as a List<string>, and pass this to the Partial View. This puts the Child Action in charge of getting it's own data. This means you only need to have this query in one place, the 'About Us' action for example doesn't need to worry about getting the list of links for the menu.

StanK
  • 4,750
  • 2
  • 22
  • 46
  • Sorry. I am not following 100%. You say that every action would need to query the database. Are you referring to the fact that the layout file would need to take in List collection meaning all other views would need to also? – chobo2 Sep 25 '12 at 22:42
  • Yes. The layout file would need to be able to pass a `List` to the Partial View. Each Action would need to populate that list from the database so that the Layout could pass the list to the Partial View. If you instead use Child Actions, the Layout kind of says 'get the data yourself' to the partial view, and doesn't need to worry about it. – StanK Sep 25 '12 at 22:56
1

Partial views Use for sharing subsections of view markup between views. Partial views can contain inline code, HTML helper methods, and references to other partial views. Partial views do not invoke an action method, so they cannot be used to perform business logic.

Child actions Use for creating reusable UI controls or widgets that need to contain business logic. When you use a child action, it invokes an action method, renders a view, and injects the result into the response stream.

0

I use Html.Action() to load dynaimc content that I do not wish to contain in the view model (for instance, user information in a sidebar). It is very useful for keeping input and output view models identical.

Note that I always use Html.Action() in conjunction with applying the ChildActionOnlyAttribute to the controller method that I am calling so that the HTML fragment is not accessible via the URL.

Mac Attack
  • 952
  • 10
  • 21
  • I edited my question. I don't understand what you mean you "I do not wish to contain in the view model". Can you please explain. – chobo2 Sep 25 '12 at 17:23
  • @chobo2 Sure. Oftentimes elements on the page do not directly relate to the main content of the page, but is still dynamic. An example would be a shopping cart sidebar on an ecommerce site; you do not want to put a summary of the cart into each view model, so instead you call `@Html.Action("Cart")` to load the summary. – Mac Attack Sep 26 '12 at 18:35
0

This question is answered (and elected for 149 times!) here:

Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction

Update Sorry, meant to send you these postings rather

Hope this helps.

Community
  • 1
  • 1
Display Name
  • 4,672
  • 1
  • 33
  • 43
  • I did see this but it seemed to really talk about what the difference between Partial/Action vs Render Partial/Action not what the difference between the 2 types are. – chobo2 Sep 25 '12 at 18:41
0

Use Html.Partial when you require a subset of your view model to render the section in question--usually it is something that has to do with what you're working on. If could be a subsection of a form, a relevant content piece related to the main view etc. A partial receives its model information from the parent view.

Otherwise use Html.Action to render content that is independent of your main view, such as a navigation piece, an aside, or other distractions. Html.Action uses its own model provided by its controller, rather than provided by the parent view.

The Prophet
  • 338
  • 5
  • 11
  • I think I am starting to get it but still not 100%. Could you not use a partial and then have a view model that is contained in another view model? For instance your menu idea. When the page loads your going to need a menu. So why hit the server twice when on first load just get everything you need and use partial? http://pratapreddypilaka.blogspot.ca/2011/11/htmlpartial-vs-htmlaction-mvc-razor.html – chobo2 Sep 25 '12 at 22:29
  • That's precisely the usage of a partial. The partial view model literally is a subset of the view model you sent to the page to begin with. When writing the piece that dictates where a partial view is to be rendered, you would reference your original model, much like Model.Menu and send it over to your partial view. Action could potentially hit the server twice depending if it requires model information to be rendered. – The Prophet Sep 27 '12 at 11:56