How do you access the current RouteData from View in .net MVC? - asp.net-mvc

If I want to directly access the RouteData (the current action or parameter values for example) from the View, rather than having to pass it in from the controller as part of the ViewData, what class would i use?

this.ViewContext.RouteData
should do the trick.

Related

Passing a model into RedirectToAction()

I'm curious how this works. In MVC you can call View() and pass a model as a parameter, but RedirectToAction (one of its incarnations at least) takes a 'routeValues' object, which appears to be the closest match.
If your model is passed in this parameter will that model type be available in the subsequent action method? Or are there caveats involved that might prevent accurate translation in some circumstances?
If you need to pass in some-what complex objects to an action after a redirect, you probably want to use either a Session or TempData:
From "What is ASP.NET MVC TempData"
ASP.NET MVC TempData dictionary is used to share data between
controller actions. The value of TempData persists until it is read or
until the current user’s session times out
By default TempData uses a Session to persist the information, however, as with much of MVC, this is an extensibility point, where you can plug in a Cookie-based provider if you prefer.
You cannot pass a model object in there but you can pass individual properties that will map to a model in the action that you redirect to.
That works by building up the url to redirect to using the properties, and the model binder in the receiving action.
Redirect... methods cause client-side-and-back trip, so - no, the model will not be available.
I think this is what you want :
Save your model in a Tempdata
RequestModel rq = new RequestModel()
....assign something to your model..
TempData["request"] = rq;
return Redirect("RequestAcknowledgement");
Now create an Action Result for the view you are redirecting to and pass your TempData back to a model. Then return the model to a view.
public ActionResult RequestAcknowledgement()
{
RequestsModel request = (RequestsModel)TempData["request"];
return View(request);
}

I have a ViewModel strongly bound to my View in my ASP.NET MVC app, now what about the controller

So like the title says, I created a view model in my asp.net mvc application so it would be strongly typed to my view. My view model is a combination of two of my model classes. Now when the user hits the save button on that view, it goes to a controller. How does it know what controller to go to? I built my controller 1 - 1 so to speak with my models and views so controller A knows about Model A and controller B knows about Model B. But if I have a view model that is AB how does it know on subit to go to A or B. Do I need a controller called AB Controller?
The controller and action invoked do not depend on the viewmodel object you used to bind the page during initial view rendering. The controller (and action) invoked is determined by the URL of the request sent. (One of the routes you have defined will be matched based on the URL string of the request or a 404 not found error will be returned.)
A submit button in an HTML form (usually a POST) will have an action attribute that determines its url target, an anchor tag will have an href, etc.
In your situation where you have a custom viewmodel object you can define an action to expect and to attempt to parse that specific type of object by specifying it as the parameter to your action:
public ActionResult SaveSystemSetting(SystemAdminVM item) {
An Action Method doesn't receive a model. It receives parameters.
Anyway I think I know where you come from: One of the parameters of your action has the type of the ViewModel used in the View. That is a common layout and makes a lot of sense. In lots of projects it is so widely used that after some time you start to think that an action actually receives a model.
Just remenber that the parameters of your action can by anything that can by filled by the ModelBinder. You could pass both Models as parameters, you could pass a ViewModel that agregates Model a and b or you could pass a ViewModel that has properties of a + b.
Agregation ist the most common approach.
Generally we overload our action methods in the controller so if you have an action called edit that renders the view with your viewmodel object, you will have an overloaded edit action method with the HttpPost specified for that method.
The data to this method will be passed as form value collection values or you can bind it to your viewmodel object if you like and process it further.

Obtain the pathName dynamically for 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.

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.

How do I get the user (IPrincipal) from a partial view in ASP.NET MVC?

On a System.Web.UI.Page.ViewPage I have access to the current User (System.Security.Principal.IPrincipal) but I don't have access to the User through a System.Web.UI.UserControl.ViewUserControl (the ViewPage property is internal).
How can my partial view (.ascx) display information depending on who the IPrincipal is?
ViewContext.HttpContext.User
HttpContext.Current.User
You can get to it via the ViewContext
ViewContext.HttpContext.User

Resources