Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Can a Partial View have a controller?

When I'm in a View and I call @Html.RenderPartial("MyPartialView", MyObject) Can I set it up so that this partial view has a controller which is called when RenderPartial gets called?

like image 458
dev.e.loper Avatar asked Jul 05 '11 20:07

dev.e.loper


People also ask

Can partial view have controller?

It does not require to have a controller action method to call it. Partial view data is dependent of parent model. Caching is not possible as it is tightly bound with parent view (controller action method) and parent's model.

How do I add a partial view controller?

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.

How do you call a partial view from a webpage or controller?

You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is true about partial view in MVC?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.


2 Answers

Probably it will be better to use the RenderAction instead of the RenderPartial

like image 90
Mikhail Avatar answered Sep 23 '22 09:09

Mikhail


You should gather all data necessary for the partial in the current controller action (which may use methods shared across other controllers and actions).

If you really want a partial to be rendered using its own controller/action then consider loading it via AJAX with a separate request.

like image 44
Wizard of Ogz Avatar answered Sep 26 '22 09:09

Wizard of Ogz