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.
Related
I need to populate a list box in a partial view, using ASP.NET MVC4.
Can Partial View have its own #model, as opposed to taking a model from its parent as described here?
I can populate my dropdown box using a separate AJAX call to another MVC controller (i.e. not parent page/url) as discussed here, but the resulting syntax is a bit more clumsy; furthermore, an additional endpoint is exposed to the outside world.
Yes - you can call partial view with any model you like. There is no requirement that data somehow comes from current model.
#Html.Partial("PartialView1", new MyOtherModel(42))
Just playing around with Sitecore 7 and MVC, and I try to get the rendering basics working.
So far, I have been able to create a View Rendering (and mapped to the relevant .cshtml file) within the Renderings section, and applied these to the presentation details of the item (in much the same way you do with ASPX Layouts/ASCX Sublayouts).
I have also been able to map the Item to a controller (using the Controller and Action fields on the item), have the Index action on the controller (inherited from SitecoreController) return the view ~/Views/Home/Index.
The issue I can't seem to wrap my head around is merging the two rendering methods. I want to be able to create controllers that map to an Item, but render the item using the ViewRenderer, rather than using the default MVC conventing of return View(), so that I can:
Specify the location of the view files within a multi-site environment by setting the path parameter of the rendering; and
Have content authors/managers manage the renderings the way that the Layout/Sublayout does with place holders.
Does anyone know of a way that this can be achieved?
Have you taken a look at Controller Renderings in Sitecore MVC? These give you the ability to map a controller class to a Sitecore presentation item that can be statically or dynamically bound to your layout details.
This post has a reasonable overview of how to get started with controller renderings.
As for specifying the location of View files for multi-site environments you can pass the path to the razor file into the Controller View method, for example:
return View("~/Areas/SampleArea/Views/SampleArea/Index.cshtml");
I hope this helps.
The well described model of View/Controller/Model is quite clear when it comes to object (say a book) update/delete/save etc...but how do you guys organize the common code such as populating drop down lists (from db)?
I use Jquery ajax to call control's action, but in cases such as getting the arrays for drop down lists, I feel like these should not reside in the same BookController .
Can I have a Controller without the matching view for these purposes only?
Thank you
Each ViewModel is data for a View to render. It sounds like you understand that. When Ajax calls for Data, I think it makes more sense for the controller for the view be responsible for creating another ViewModel and returning it as Json for the rendered view. If mutliple views need to retrieve a list of Books, should call /Books/AjaxList (bad method name example), just like any view under /Books. Seperating the responsibility of creating a ViewModel based on Ajax or not Ajax doesn't make sense to me.
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).
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.