nosedive_89
nosedive_89

Reputation: 51

Creating Url.Action causes URL string to build incorrectly

I have a button which has an onclick function of onclick="location.href='@Url.Action("SendRegisterLink?Email="+ item.Email, "Account")'"

The button works, the URL builds, and I have some JS which grabs the "Email" field from the URL fine - but I am not getting the correct output:

EXPECTED URL

/Account/[email protected]

ACTUAL URL

/Account/SendRegisterLink%3fEmail%3dtest%40test.com

Upvotes: 1

Views: 597

Answers (2)

Neville Nazerane
Neville Nazerane

Reputation: 7019

Have a look at the actual parameters of the functions. You can use ctrl + click in the latest vs to check its params.

public static string Action(this IUrlHelper helper, string action, string controller, object values);

If you want to use the Action function you can use that overload. The first parameter needs to be only your action name as a string name. So you can use it like this:

@Url.Action("SendRegisterLink", "Account", new { item.Email })

If you want to mention a URL with the parameters directly, you can use the Content function:

@Url.Content("~/Account/SendRegisterLink?Email=" + item.Email)

If your intention is just a redirect you can also something clean with an a tag:

<a href="~/Account/[email protected]">My Link</a>

You can always change the css of anchor tags.

Upvotes: 1

Chirag Rupani
Chirag Rupani

Reputation: 1715

You need to use

@Url.Action("SendRegisterLink, "Account", new { Email= item.Email});

Upvotes: 2

Related Questions