Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 767
Randika Wijekoon Avatar asked Jun 29 '26 14:06

Randika Wijekoon


1 Answers

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.

like image 194
Cacho Santa Avatar answered Jul 02 '26 13:07

Cacho Santa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!