Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

@Html.Partial("_Testimonials") 
like image 800
brenjt Avatar asked Jun 09 '11 01:06

brenjt


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 return a partial view from controller?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How do you call a partial view in another view in MVC?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

Can We Make Controller as partial in MVC?

Yes, we can make controller as partial. Lets create a mvc application. Now create a contolller and name it "ABCController". Now make the controller as partial. Now add a view and name it Index. Now add another controller and name it xyzController.

How partial views are created in ASP NET MVC?

How Partial Views are Created in ASP.NET MVC Application? Right-click on the /Views/Shared folder and Select Add -> View option from the context menu and then provide the following details Check the Create a partial View check box and click on Add button as shown in the below image

How to use partialview function in MVC Razor?

Here Mudassar Ahmed Khan has explained with an example, how to use the PartialView function i.e. return Partial View from Controller in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html.Action function in ASP.Net MVC Razor.

How to add partial view in Salesforce controller?

The name of the Action method i.e. Details and its parameter i.e. CustomerId are passed to the @Html.Action function. 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.


2 Answers

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"); %> 
like image 106
George Stocker Avatar answered Sep 21 '22 17:09

George Stocker


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.

like image 41
Slick86 Avatar answered Sep 19 '22 17:09

Slick86