tags:

views:

48

answers:

2

hi everyone,

i m using default route of mvc2 like {controller}/{action}/{id}. i want to access that id field in view. how can i do that without using tempData and ViewData. if i have url like http://server-name/home/edit/14 i need value 14 in aspx view

any help is highly appreciated
regards
Adeel

+5  A: 

You can access route data using the ViewContext property:

<%= ViewContext.RouteData.Values["controller"] %>
Matthew Abbott
+1  A: 

This question may help...

http://stackoverflow.com/questions/151963/asp-net-mvc-how-do-i-get-virtual-url-for-the-current-controller-view

Curious, why don't you want to use ViewData/ViewModel?

Daz Lewis
Thanks Daz for quick reply. infact i don't like ViewData approach and use it only when i have no other options. ViewModel is a sophisticated approach however but i have more than 80 pages and i m not in favor of creating viewmodel for a value that can be plucked from route value dictionary unless someone tells me its dangerousregards
Muhammad Adeel Zahid
If you have 80+ pages wanting to use the Id field I'd recommend a base model populated off and overriden OnActionExecuted in a custom base controller.http://stackoverflow.com/questions/2751225/how-to-use-a-base-viewmodel-in-asp-net-mvc-2
Daz Lewis
thanks daz i believe that's a great idea. i will consider it when i get back on monday. have nice weekendregards
Muhammad Adeel Zahid
hi daz can u plz refer some example of base model being populated in onActionexecuted. i really have no idea how to do it regards
Muhammad Adeel Zahid