Jeremy
Jeremy

Reputation: 1908

Html.ActionLink in asp.net MVC object value in wrong format

I have a html.actionlink that i wish to display a link to a members profile page like this: http://somesite.com/members/{username}

When use the following markup

<%= Html.ActionLink(r.MemberName, "profile", new { MemberName = r.MemberName } )%>

I get a link that looks like this: http://somesite.com/members?MemberName={username}

What would i need to change in the ActionLink helper to achieve a url like this:

http://somesite.com/members/{username}

Upvotes: 4

Views: 5265

Answers (3)

Jeremy
Jeremy

Reputation: 1908

Thanks for both your responses... I did not have my route matching the value name.

Simply ensuring that my route url matched made it work.

Here's my code....

//Global.asax
routes.MapRoute(
    "Profile",
    "members/{membername}",
    new { controller = "Members", action = "Profile", memberName = "" }
);

//In the Controller
public ActionResult Profile(string memberName)
{
  return View();
}

//My Action Link
<%= Html.ActionLink(r.MemberName, "profile", new { memberName = r.MemberName })%>

Thanks again

Upvotes: 2

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

You should add the route that maps "/members/{MemberName}" before other routes in the routing table.

Upvotes: 2

John Sheehan
John Sheehan

Reputation: 78124

Assuming in your routes the username token is {username} like you show, try this:

<%= Html.ActionLink(r.MemberName, "profile", new { username = r.MemberName } )%>

Upvotes: 4

Related Questions