Creating a controller for partial viewing in ASP.NET MVC

How to create a separate controller and model for partial viewing? I want to be able to post this partial presentation anywhere on the site so that it needs its own controller. I am currently doing partial as

@Html.Partial("_Testimonials") 
+48
asp.net-mvc partial-views
Jun 09 '11 at 1:00 a.m.
source share
4 answers

Why not use Html.RenderAction() ?

Then you can 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 can create a new partial view and have the PartialViewModel that it inherits from.

For Razor, the code block in the view will look like this:

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

For WebFormsViewEngine, it will look like this:

 <% Html.RenderAction("Index", "Home"); %> 
+87
Jun 09 '11 at 1:10
source share

He does not need his own controller. you can use

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

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

+7
Jun 09 '11 at 1:10
source share

If it were me, I would just create a new controller with a single action, and then use RenderAction instead of Partial:

 // Assuming the controller is named NewController @{Html.RenderAction("ActionName", "New", new { routeValueOne = "SomeValue" }); } 
+5
Jun 09 2018-11-11T00:
source share

Most importantly: the created action should return a partial view, see below.

 public ActionResult _YourPartialViewSection() { return PartialView(); } 
+2
Mar 14 '13 at 16:10
source share



All Articles