Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling a partial view in asp.net MVC3

What is difference between Html.Partial and Html.action in context of using a partial view in asp.net MVC3 ?

like image 442
DotnetSparrow Avatar asked Apr 07 '11 16:04

DotnetSparrow


People also ask

How can we call a partial view in view of ASP NET core?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How do you reference partial view?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

How do you call a partial view in another partial view?

To call a partial view from another partial view, we follow the same approach as we follow when we call partial view from the content page. Let's take an example. This is the code of 1st partial view. Notice that in above partial view, we are calling another partial view named "_View2" that is created below.

How do you call a partial view inside a view in MVC 4?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.


1 Answers

Html.Action will call a controller Action, so it'll go through the whole MVC pipeline (inside the server) again to find a controller that will return a ViewResult (although, you theoretically you can also return a JsonResult or something else) .

Html.Partial will only return a PartialPage (as in a CSHTML file) and won't go through the whole pipeline. It will just search using the view engine.

Some advantages to Action is having Authentication, Caching and other stuff that happens in the MVC pipeline, while Partial is faster (although you might have more responsibility in the partial page if you need to pass a ViewModel etc.)

This is a nice post (a bit old) about pros/cons of RenderAction vs RenderPartial

like image 178
Linkgoron Avatar answered Oct 15 '22 20:10

Linkgoron