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.
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.
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.
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.
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).
You can access route data using the ViewContext property:
<%= ViewContext.RouteData.Values["id"] %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With