MST QNB MST QNB - 1 year ago 114
ASP.NET (C#) Question

How can I pass two parameters from ActionLink to ActionResult

I'm implementing a website using MVC. I'm trying assign role to user by clicking into the role. I've tried several ways, unfortunately, I failed.

Controller method

[CustomAuthorize(Roles ="SudoAdmin")]
public ActionResult AssignUserToRole(long userId, long roleId)
new BusinessUser().AddRoleToUser(userId, roleId);

return RedirectToAction("Index");


@Html.ActionLink(@r.RoleName, "AssignUserToRole", "Users", new { userId = Model.UserId, roleId = r.RoleId })

first I tried Jquery Ajax, but it failed, even though I think it was not a very great idea. However, I still would like to do it with "Ajax"I mean without refreshing the page.

I hope my question is good enough
thanks for all of you

Answer Source

First, the action is set to respond only to POST. That means unless you request it via POST, you'll get a 404, since there's no action that can respond to any other request method.

As a link, it will always make the request via GET. If you employ JavaScript, you can bind to the click event and send the request via POST using AJAX, but again, you have to send it as POST. If you use something like $.get or $.ajax with the method as GET, it won't work.

Ideally, if you intend to hit the action via POST, then you should employ a form element in your view. Using CSS, you can style the submit button to look like a link, if that's what you want, but the underlying structure should be a form.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download