Tuesday, May 3, 2011

How to get the ID from URL?

I am using asp.net MVC.

I have edit page url like {controller}/Edit/2

so on the view page how can I get the ID from this URL?

I'm gonna put a link to redirect to some page with sending above ID.

EDIT Like

<%=Html.ActionLink("name", "Action", "Controller", new{ ID = ? } ) %>
From stackoverflow
  • I know of two ways: You can create your link and use the "ViewData" property to pass the link to your view or you can stronglyType the ViewPage.

    Here is a link on strongly typing the view.

    Vikas : Not in the controller! but on view page!
    Chuck Conway : My bad, misread the question
  • Put the ID in the ViewData in your action method, then your view can access the value from the ViewData.

    Controller: ViewData["ID"] = id;

    View: <%=Html.ActionLink("name", "Action", "Controller", new{ ID = (int)ViewData["ID"]} ) %>

  • you can get it from the RouteData object

     <%=Html.ViewContext.RouteData.Values["id"].ToString() %>
    

0 comments:

Post a Comment