gog
gog

Reputation: 12988

Cannot resolve symbol @Url.Action

Should be a simple problem but i dont know exactly why its like this.

In my ASP.NET MVC 5 website i have a simple view with a grid, and a cell action that calls a js function sending some parameters to this function.

function OnCellClick(param1, param2) {
        var urlAJAX = @Url.Action("GetJson", "PosicaoEstoque", new { p1 = param1 , p2 =param2}); }

So, like this i get the 'Cannot Resolve symbols' for the param1 and param2. How can i solve it?

Upvotes: 0

Views: 1187

Answers (3)

Lin
Lin

Reputation: 15188

Without using Url.Action like below:

var urlAJAX = 'PosicaoEstoque/GetJson?p1=' + param1 + '&p2=' + param2;

Upvotes: 1

Konrad Gadzina
Konrad Gadzina

Reputation: 3404

Your problem is that you're mixing server-side and client-side code. You cannot simply put JavaScript variables in Url.Action(), as it runs on the server-side. What you can do is to put some dummy values as parameters and call JavaScript's replace() function on generated URL.

Check this for reference.

Upvotes: 1

Satpal
Satpal

Reputation: 133403

You can use placeholder. Generate url using place holder parameters and then replace them with param

function OnCellClick(param1, param2) {
    var urlAJAX = '@Url.Action("GetJson", "PosicaoEstoque", new { p1 = -1 , p2 = -2})';
    urlAJAX = urlAJAX.replace('-1', param1).replace('-2', param2);
}

Upvotes: 4

Related Questions