Search code examples
razorasp.net-mvc-5

ActionLink doesn't work


I'm pretty sure that I'm doing something really stupid. Please have a look and let me know what I'm doing wrong.

Here is my ActionLink

@Html.ActionLink("Edit","Edit","UserProfile", new { id = Model.ApplicationUserId },null)

When I click this it throws the bad request exception and also I noticed the url is

https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b

not

https://localhost:44304/UserProfile/Edit?userId=92b1347c-c9ec-4728-8dff-11bc9f935d0b

I have a HTTPGET Edit method in my controller and it takes UserId. When I pass the route values manually it works.Please help me. Your help is much appreciated and someday, will pay it forward. Thanks! Cheers!


Solution

  • If the parameter you are expecting is userId, then use the @Html.ActionLink like this:

    @Html.ActionLink("Edit","Edit","UserProfile", new { userId = Model.ApplicationUserId },null)
    

    If you pass the parameter with name id, then the MVC will route like you mentioned:

    https://localhost:44304/UserProfile/Edit/92b1347c-c9ec-4728-8dff-11bc9f935d0b
    

    Which is great, but your method should be something expecting the parameter with the appropriate name:

    // GET: /UserProfile/Edit/{id}
    public ActionResult Edit(String id){
    
    
         //your code
         return View();
    }
    

    If you have some time, check out this ASP.NET MVC Routing Overview with a lot more details.