Obtain the pathName dynamically for ASP.NET MVC - asp.net-mvc

I'm looking for a solution that allows me to get the parent page pathname from a partialView invoked. For example I have an Action baa and a controller boo so the javascript to obtain this will be window.location.pathname, and it returns /boo/baa.
The thing is that I use a partial view, so when I try to retrieve the pathname via the URL helper, what I obtain is the PartialView one, and what I need is the pathname for the action that invokes it. I want to do this because I want to show certain things of the partial view depending on the pathname of the parent view that invokes it. Is there a way to do this with ASP.NET MVC?
Thank you all in advance,

You can access the RouteValues on the ViewContext on the page. This will contain the controller and action values as determined by the routing engine. You might want to consider making the determining information part of your model, however, as this exposes logic in the controller -- what the view does with this information, though, is completely up to it.

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.

generate route url's in views or controllers

I am currently working with mvc4 and have a question around best practice.
I am passing back to my view, a number of links based on product information eg. product/1234 etc.
What is best practice, create the link using the routing engine in the controller and return the url as a property on the model object OR return the information to the view and generate the link there? I use automapper to map my DTO objects to model object, also considering creating the links during mapping.
What is the best practice with this?
You always create the link on the view.
The HTML helpers in the view can be used to ensure the link conforms to your routing rules.
You can see this in action in the many official ASP.NET MVC 4 Tutorials.
Why not in the controller or model?
The HTML helpers in the view are designed to create not just the URL, but also to wrap the URL in a fully formed anchor tag, etc. It isn't appropriate to have HTML in your model or controller as they shouldn't care how the data is displayed.
For example, the Html.ActionLink helper returns an a element.

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).

How to find out the current resource and controller name from the view in ASP.NET MVC

I'm using the ASP.NET MVC in a RESTful resource manner and I want to be able get the current resource and controller name from the view.
I'm attempt to create a HTML page and I want to know the current resource and controller name is it possible to get this?
you can get that in a view by looking at the ViewContext.Controller property. The ViewContext property also gives access to many other useful properties such as Route Data, Application, Cache, ViewData, etc.
EDIT: To get the actual name of the controller you can go one of two ways:
1) Call GetType() on the Controller property of ViewContext and use that Name property to get the class name of the controller
2) Look at the route data and examine the values for the "controller" key, e.g. ViewContext.RouteData.Values["controller"] (this would likely be the preferred method)
This information is available in the ViewContext property of ViewPage (assuming you are using .aspx for your views).
You can always run GetType while in the controller (or just type it in if thats what you want to do) and store it in the view data. If you are using the same view from multiple controllers, you might want to make it a strongly typed view and have the controller name be part of it.
Can you be more specific with why exactly you want to do this? There might be a better way.

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