[asp.net] Create controller for partial view in ASP.NET MVC

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


This question is related to asp.net asp.net-mvc partial-views

The answer is

Why not use Html.RenderAction()?

Then you could put the following into any controller (even creating a new controller for it):

public ActionResult MyActionThatGeneratesAPartial(string parameter1)
    var model = repository.GetThingByParameter(parameter1);
    var partialViewModel = new PartialViewModel(model);
    return PartialView(partialViewModel); 

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:

@{ Html.RenderAction("Index", "Home"); }

For the WebFormsViewEngine, it would look like this:

<% Html.RenderAction("Index", "Home"); %>

Similar questions with asp.net tag:

Similar questions with asp.net-mvc tag:

Similar questions with partial-views tag: