views:

627

answers:

2

I have the following code in my site.master for a menu:

<ul id="menu">              
    <li><%= Html.ActionLink("My Contact Info", "DetailsbyUserName/" + Html.Encode(Page.User.Identity.Name), "Users")%></li>
</ul>

When I hover over the URL I see that it points to:

http://site/Users/DetailbyUserName/[name]

which is correct.

The issue is that when I put a breakpoint in the Users controller class below:

public ActionResult DetailsbyUserName(string loginName)
{
    UserInfo user = repo.GetUserByName(loginName);
    return View(user);
}

it seems that the loginName parameter is always null.

Any suggestions?

A: 

try:

Html.ActionLink("My Contact Info", "DetailsbyUserName", "Users", new { loginName = Html.Encode(Page.User.Identity.Name), null);

if your controller is something like this:

public ActionResult DetailsbyUserName(string veryVeryLongParameterName);

I guess you have to use new { veryVeryLongParameterName = "YourParamValue" } in the ActionLink's routeValues parameter.

And also, you need a route for that.

I'm very new to this too, at least that's what I understood about ActionLinks, hope someone can explain it better.

Fabio Gomes
+3  A: 

The problem is that you don't have a Route set up to specify a parameter called "loginName".

I'm guessing that your default Route is swallowing your request, and trying to assign the [name] value to a parameter called "id". If you change the name of the parameter to "id" from "loginName", I bet it will work for you.

Remember that the routing engine maps each URL segment to a named parameter. The default route looks like: "{controller}/{action}/{id}". If you want to have a parameter named "loginName", you would have to come up with a Route that had the segments "{controller}/{action}/{loginName}", that was different from the default route, so that the default route didn't match it first.

womp