102

I can't seem to retrieve an ID I'm sending in a html.ActionLink in my controller, here is what I'm trying to do

<li>
    <%= Html.ActionLink("Modify Villa", "Modify", "Villa", new { @id = "1" })%></li>


    public ActionResult Modify(string ID)
    {

        ViewData["Title"] =ID;
        return View();
    }

That's what a tutorial I followed recommended, but it's not working, it's also putting ?Length=5 at the end of the URL!

Here is the route I'm using, it's default

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
E_net4
  • 27,810
  • 13
  • 101
  • 139
asleep
  • 4,054
  • 10
  • 34
  • 51

6 Answers6

207

Doesn't look like you are using the correct overload of ActionLink. Try this:-

<%=Html.ActionLink("Modify Villa", "Modify", new {id = "1"})%>

This assumes your view is under the /Views/Villa folder. If not then I suspect you need:-

<%=Html.ActionLink("Modify Villa", "Modify", "Villa", new {id = "1"}, null)%>
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 5
    Good call - The problem was that the first overload of that function took "HtmlAttributes" as the fourth parameter. So adding ", null" forced the compiler to use your inline object as the route parameters. – Timothy Khouri Nov 25 '08 at 11:21
31

In MVC 4 you can link from one view to another controller passing the Id or Primary Key via

@Html.ActionLink("Select", "Create", "StudentApplication", new { id=item.PersonId }, null) 
MUG4N
  • 19,377
  • 11
  • 56
  • 83
Oracular Man
  • 1,060
  • 11
  • 15
14

Don't put the @ before the id

new { id = "1" }

The framework "translate" it in ?Lenght when there is a mismatch in the parameter/route

Davide Vosti
  • 2,485
  • 2
  • 22
  • 31
6

On MVC 5 is quite similar

@Html.ActionLink("LinkText", "ActionName", new { id = "id" })
César León
  • 2,941
  • 1
  • 21
  • 18
3

The ID will work with @ sign in front also, but we have to add one parameter after that. that is null

look like:

@Html.ActionLink("Label Name", "Name_Of_Page_To_Redirect", "Controller", new {@id="Id_Value"}, null)
A J
  • 3,970
  • 14
  • 38
  • 53
ebsom
  • 31
  • 1
  • 6
0

If the target action requires a parameter, you can use an anonymous object to pass parameter values:

@Html.ActionLink(“View Movies”, “Index”, “Movies”, new{id=1},null)  

Where:

-View Movies-->String LinkText

-Index--> string ActionName

-Movies-->string ControllerName

-new{id=1}-->(object) Is the parameter values that you want to pass

-Null-->object htmlAttributes

*For Example:

This should generate a link like the following:

/movies/index/1