ASP.NET MVC - Can a Partial View have a controller? - asp.net-mvc

When I'm in a View and I call #Html.RenderPartial("MyPartialView", MyObject) Can I set it up so that this partial view has a controller which is called when RenderPartial gets called?

Probably it will be better to use the RenderAction instead of the RenderPartial

You should gather all data necessary for the partial in the current controller action (which may use methods shared across other controllers and actions).
If you really want a partial to be rendered using its own controller/action then consider loading it via AJAX with a separate request.

In MVC, although controllers know about views, the reverse is not true.
Views are just means to render some data (a model or a viewModel) but they are not related to a controller or an action.

Related

Using #Html.Partial In Sitecore

I would like to use the generic razor helper function Html.Partial to render views that have common html in them.
For instance, I have two views set up in Sitecore Payment Information.cshtml and Agent Payment Information.cshtml. These are rendered using the Sitecore rendering engine. Both of these views have very similar html in them that I would like to put in razor views not set in Sitecore and call them with #Html.Partial as appose to #Html.Sitecore().Rendering() as the latter forces me to set up a view and model in Sitecore which I am not sure is necessary.
My question is, is there anything that Sitecore does behind the scenes that makes it necessary to usethe #Html.Sitecore().Rendering() helper method instead of the #Html.Partial() helper method? Everything seems to work fine and I believe the entire view should get cached since the #Html.Partial call is nested inside either the Payment Information view or the Agent Payment information view set up in Sitecore.
Thanks in advance.
I have Html.Partial working in an MVC solution using Glass for ORM. There are two ways I've used this, one where the assumed model being passed to the partial is the same as the parent rendering and another where we create the model on the fly.
Assumes parent rendering model is passed:
#Html.Partial("~/Views/Components/MyPartialView.cshtml")
Instantiates a new model that is passed in:
#Html.Partial("~/Views/Components/Navigation/SecondaryNavigationRendering.cshtml", new SecondaryNavigation())
The parent view will need to have a mapped model in Sitecore. The secondary view does not have a mapped model in Sitecore but is typed to receive the model being passed (so in my first example that would be my IBasePage model, in my second it would be my SecondaryNavigation model).
Hope this helps.

How to Load Content into an ASP.NET MVC Partial View in a Layout View

'm using ASP.NET MVC 3 with Razor views. I have a partial view which I would like to render on all pages so I am placing it in the site's main Layout page. However, I am not sure the best way to load data into the partial view. I could load it for each ActionMethod but there is there a way to do this globally across the entire app?
Move all the data loading logic for your partial into a separate action method.
Then in your layout page, instead of rendering the partial with a call to RenderPartial(), call the RenderAction() method.
RenderAction() makes a "child" action call - thus putting all the logic needed for that partial into one place.
Write action for this partial view in MasterController because every controller inherits from it, and place your partial view in shared folder and call it on Site's master page (like every site has a user control which gives login box until user is logged in else it displays logged in user info) ... hope it answer your ques ...

calling a partial view in asp.net MVC3

What is difference between Html.Partial and Html.action in context of using a partial view in asp.net MVC3 ?
Html.Action will call a controller Action, so it'll go through the whole MVC pipeline (inside the server) again to find a controller that will return a ViewResult (although, you theoretically you can also return a JsonResult or something else) .
Html.Partial will only return a PartialPage (as in a CSHTML file) and won't go through the whole pipeline. It will just search using the view engine.
Some advantages to Action is having Authentication, Caching and other stuff that happens in the MVC pipeline, while Partial is faster (although you might have more responsibility in the partial page if you need to pass a ViewModel etc.)
This is a nice post (a bit old) about pros/cons of RenderAction vs RenderPartial
Html.Partial includes directly the view at the place where you called the helper. It is like a file include.
Html.Action invokes a controller action first which could render a view and it is the result of this action that is included. And because a controller action is invoked a controller needs to be instantiated, so the whole MVC pipeline gets executed as a child request.
You may take a look at the following blog post.
Stackoverflow ninja search got me this:
Html.Partial vs Html.RenderPartial & Html.Action vs Html.RenderAction
Also see this article
Render Action vs RenderPartial

ASP.Net MVC reusable form as RenderAction or RenderPartial

I'm looking for a best practice for embedding a form on multiple pages as a partial view.
I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.
How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.
Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.
The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:
Manually add it to the response of
the current action
Manually add it to the response of
the current controller by way of a
base controller that modifies the
ViewState or ViewModel
Call the RenderAction()
HtmlHelper inside of the current view
Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.
Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).

Populating the model for a shared view, embedding a shared view within another view

I'm trying to embed a small view snippet that steps through a model fragment that works fine when I embed it in a single controller and pass it to a view like so;
Controller:
return View(_entities.formTemplate.ToList());
View:
http://www.pastie.org/666366
The thing is that I want to be able to embed this particular select box in more than just this single action / view, from the googling I've been doing this appears that it should go into a shared view, but I'm not clear then on how I could populate the model within that view from the controller? (or maybe I'm completely missing the purpose for shared views?)
In the other MVC framework I'm accustomed to working with there is the concept of a filter where you can call code before or after any action and mod the model as it passes the controller and goes to the view, is such a thing possible in .net mvc?
Any assistance appreciated.
You'll want to use the HtmlHelper method DropDownList() in order to create a input:
<%= Html.DropDownList("id", new SelectList(formBuilder, "ID", "Name")) %>
You probably want to use a ViewUserControl here.
You have a couple of options if you go that route. If it's model data that is easily available, recreate it at the call site of your RenderPartial like so:
<%=Html.RenderPartial("ViewName", new ModelData())%>
If it's data that is dependent on the current model data, then you'll need to pass that data somehow to your partial view.
ASP.Net MVC also has the concept of before/after controller actions. You decorate your controller method with an Attribute that derives from ActionFilterAttribute. In there, you have access to OnActionExecuting and OnActionExecuted.

Resources