How can I create an individual controller and model for a partial view? I want to be able to place this partial view any where on the site so it needs it's own controller. I am current rendering the partial as so
@Html.Partial("_Testimonials")
How can I create an individual controller and model for a partial view? I want to be able to place this partial view any where on the site so it needs it's own controller. I am current rendering the partial as so
@Html.Partial("_Testimonials")
You don't need a controller and when using .Net 5 (MVC 6) you can render the partial view async
or
or if you are using .net core 2.1 > you can just use:
If it were me, I would simply create a new Controller with a Single Action and then use RenderAction in place of Partial:
It does not need its own controller. You can use
This allows you to render the partial from any page. Just make sure the relative path is correct.
Html.Action is a poorly designed technology. Because in your page Controller you can't receive the results of computation in your Partial Controller. Data flow is only Page Controller => Partial Controller.
To be closer to WebForm UserControl (*.ascx) you need to:
Create a page Model and a Partial Model
Place your Partial Model as a property in your page Model
In this situation you can use it like:
P.S. In step 3 you can use Html.Partial("PartialViewName", Model.MyPartialModel, <clone_ViewData_with_prefix_MyPartialModel>). For more details see ASP.NET MVC partial views: input name prefixes
Why not use
Html.RenderAction()
?Then you could put the following into any controller (even creating a new controller for it):
Then you could create a new partial view and have your
PartialViewModel
be what it inherits from.For Razor, the code block in the view would look like this:
For the WebFormsViewEngine, it would look like this:
The most important thing is, the action created must return partial view, see below.