Rivera
Rivera

Reputation: 79

generate URL with @Url.Action

I have a question about Url.Action.

My position is on http://localhost/User/Edit and for some case I have to generate a link with a javascript function, so it would be like this:

 return '<a href="@Url.Action("Group","Edit")/' +myParameterInJavascript +'>link</a>';

If I look to the link, it would be ok, I got: http://localhost/Group/Edit/ParameterFromJs
But, then when my current position is http://localhost/Group/Edit/ParameterFromJs and I generate the same link again, the URL will become :

http://localhost/Group/Edit/ParameterFromJs/ParameterFromJs

Why don't I just get the url http://localhost/Group/Edit/ParameterFromJs? Why was my action Edit/ParameterFromJs, and not just Edit?

Can you give me some hint or tips? Thanks in advance

UPDATE : This is my routing:

routes.MapRoute("group-edit",
            "Group/Edit/{groupName}",
            new
            {
                controller = "Group",
                action = "Edit"
            }
        );

Upvotes: 1

Views: 12740

Answers (2)

Kamil Będkowski
Kamil Będkowski

Reputation: 1092

Try to use something like that:

<a href="@Url.Action("Edit", "Group", new { EditParam = myParameterInJavascript })">

When You put parameter use "?" not "/"

 http://localhost/Group/Edit?ParameterFromJs

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Try using routing:

var url = '@Url.Action("Group", "Edit", new { id = "__id__" })'.replace('__id__', myParameterInJavascript);
return '<a href="' + url + '">link</a>';

Upvotes: 6

Related Questions