How to get the current route ID within a View from the URL (ASP.NET MVC)

ViewData is exactly the right way of doing this.

Your other option would be to pass a model that contains ID to the view.

Edit: Without knowing exactly what you're tying to do, it's tough to give more specific advise. Why do you need an ID, but not any other model data? Is your controller really only sending the Id field to the view? It's hard to imagine what the scenario is.

If the ID value is truly the only model information that is being passed to your view, then you could use the ID itself as the model. Then the return value of your action method would be View(id) and you wouldn't need to use ViewData.


I think this is what you're looking for:

<%=Url.RequestContext.RouteData.Values["id"]%>

Tags:

Asp.Net Mvc