Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use Html.Action over Html.Partial

Tags:

c#

asp.net-mvc

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?

like image 424
chobo2 Avatar asked Sep 25 '12 16:09

chobo2


People also ask

What is the difference between HTML partial info and HTML action info?

As I understand it, you use Action when calling a controller action because it lets you specify the Controller as well as the Action to use from the controller. Partial and RenderPartial both let you just specify a View name and MVC magically find the right view and renders it.

What is the difference between HTML action and HTML RenderAction?

The difference between the two is that Html. RenderAction will render the result directly to the Response (which is more efficient if the action returns a large amount of HTML) whereas Html. Action returns a string with the result.

What is the difference between HTML partial vs HTML RenderPartial and HTML action vs HTML RenderAction?

Partial injects the html string of the partial view into the main view. Html. RenderPartial writes html in the response stream. Here is what I have found:Use RenderAction when you do not have a model to send to the view and have a lot of html to bring back that doesn't need to be stored in a variable.

What are the benefits of HTML RenderPartial over?

B) @Html. RenderPartial Returns nothing (void), it is faster than @Html. Partial, moreover requires not to create action.


2 Answers

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.

like image 174
Nick Albrecht Avatar answered Sep 29 '22 18:09

Nick Albrecht


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.

like image 28
StanK Avatar answered Sep 29 '22 19:09

StanK