Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing route values in view Mvc.net

I'm using default route of mvc2 like {controller}/{action}/{id}. I want to access that id field in my view. How can I do that without using TempData and ViewData?

If I have a url like http://server-name/home/edit/14 I need the value "14" in the view.

like image 531
Muhammad Adeel Zahid Avatar asked Jul 01 '10 09:07

Muhammad Adeel Zahid


People also ask

How do you find a route value?

The route values are stored as string s, not int s, so we still need to convert them. We have routing but not model binding. GetRouteData() gives access to the whole RouteData object, so we can also access other data like data tokens. Alternatively, you can use GetRouteValue() to access a single route value at a time.

What is route values in MVC?

RouteData is a property of the base Controller class, so RouteData can be accessed in any controller. RouteData contains route information of a current request. You can get the controller, action or parameter information using RouteData as shown below.

How can use route in ASP.NET MVC?

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

Where we define routes in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).


1 Answers

You can access route data using the ViewContext property:

<%= ViewContext.RouteData.Values["id"] %>
like image 158
Matthew Abbott Avatar answered Oct 17 '22 23:10

Matthew Abbott