This project is read-only.

Can't invoke an action method with parameter via href

Topics: Developer Forum
Nov 11, 2009 at 5:44 AM

In the sample appl. the ManageUser.aspx contains a list of user in a table. (p. 142). Every row contains a href link to the User/EditUser action, that picks up a memebership.UserName for id parameter as querystring, and invokes the method with the parameter:

p. 143.

<td><a href="EditUser?id=<%= membershipUser.UserName %>"><img border="0" alt="Edit User" src="/content/images/EditSymbol.png" title="Modify User" align="middle"/></a></td>

I tried to implement this code snippet in my own appl., but while this link is working in the TheBeerHouse application, it doesn't work in mine.

In my application the link invokes EditUser method without picking up the user id from the membershipUser.Username. When I move the cursor on the hyperlink of the user's table (ie. on the edit symbol), in the status bar I see the right route: User/EditUser?id=Admin, but call the method only User/EditUser without parameter.

When I trace the html, the membershipUser.Username has a value, and the delete action method works well also, that uses the same value.

Does anybody can help me, what is missing in my application, while I'm using the same code and similar web.config file as original?

Thanks in advance


Nov 11, 2009 at 2:22 PM

Have you used firebug, use firebug to make sure the request is going to the right place. Remember if you have this in a virtual directory the delete request is going to go to:

instead of

which I am guessing is your issue.

Nov 11, 2009 at 2:58 PM

The deleteUser works well, and the request to EditUser is going to the right Action method, but without parameter.

So the problem is, why the request loses the parameter?



Nov 12, 2009 at 3:02 PM
Edited Nov 12, 2009 at 9:29 PM

Response to my own question:

The request loses the parameter, beacasue we haven't matching Url Routing.

The default route: 

"Default","{controller}/{action}/{id}",new { controller = "Home", action = "Index", id="" } 

doesn't mutch the request:

<a href="EditUser?id=<%= membershipUser.UserName %>">Edit</a> and we can't create a matching one, because inserting the "?" character inside the route pattern isn't allowed.

This works in the TheBeerHouse application, because the default Url routing contains nothing:

"Default","{controller}/{action}",new { controller = (string)null, action = (string)null } 

But in the sample application we can't request a user writing the ordinary Url like this:


 we have to write:


instead, that is differs from any other url route in the application.

A better solution to use the built in html helper - RouteLink:

<%= Html.RouteLink("Edit User","ForQueryString", new{id=membershipUser.UserName}) %>

 that automatically converts to the following link:

<a href="localhost:4962/User/EditUser/Admin">Edit User</a>

The ForQueryString route is:

"ForQueryString","User/EditUser/{id}",new { controller = "User", action = "EditUSer", id = "" }

This solution has a drawback, we can't add a picture to the link. 

The Url.RouteUrl helper method come to rescue, that returns only url, without the anchor tag. We have to put this expression inside the anchor, and voila, the link works as expected, and we don't have to use the ugly querystring syntax in the url.

<td><a href="<%= Url.RouteUrl("ForQueryString", new{id=membershipUser.UserName})%>"><img border="0" alt="Edit User" src="/content/images/EditSymbol.png" title="Modify User" align="middle"/></a></td>

I think, using a builtin functionality give us a more elegant simpler solution, that corresponds to the url routing standard.






Apr 2, 2010 at 1:01 PM

Thanks Gabor , much apprciated Edit User resolution. You saved me dude. Great explantion