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

Create controller for partial view in ASP.NET MVC


Why not use Html.RenderAction()?

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

[ChildActionOnly]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"); %>


It does not need its own controller. You can use

@Html.Partial("../ControllerName/_Testimonials.cshtml")

This allows you to render the partial from any page. Just make sure the relative path is correct.


If it were me, I would simply create a new Controller with a Single Action and then use RenderAction in place of Partial:

// Assuming the controller is named NewController@{Html.RenderAction("ActionName",                      "New",                       new { routeValueOne = "SomeValue" });}