Pass variable as parameter in Url.Action in javascript

I pass the parameters @ Url.Action as follows:

function showHistory() { myId= $("#id").val(); //alert(myId); actionDialog.load("@Url.Action("ActionHistoryAjax", new {id = myId, sort = "abc"})", function () { actionDialog.dialog('open'); }); } 

But it gives an error "the name myId does not exist in the current context".

How can I pass a variable?

I solved this, this solution:

 function showHistory() { myId= $("#id").val(); //alert(myId); actionDialog.load("@Url.Action("ActionHistoryAjax", new {id = "_Id", sort = "abc"})".replace("_Id", DeviceID), function () { actionDialog.dialog('open'); }); } 
+6
source share
3 answers

I solved this using this:

 function showHistory() { myId= $("#id").val(); actionDialog.load("@Url.Action("ActionHistoryAjax", new {id = "_Id", sort = "abc"})".replace("_Id", DeviceID), function () { actionDialog.dialog('open'); }); } 
+8
source

Here you mix server and client code. You cannot pass a Javascript variable to the Url.Action method.

You will need to do something like:

 function showHistory() { myId= $("#id").val(); actionDialog.load("@Url.Action("ActionHistoryAjax", new { sort = "abc"})" + "&id=" + encodeURIComponent(myId), function () { actionDialog.dialog('open'); }); } 

Or load this identifier from the server model.

+2
source
 old one function showHistory() { myId= $("#id").val(); //alert(myId); actionDialog.load("@Url.Action("ActionHistoryAjax", new {id = myId, sort = "abc"})", function () { actionDialog.dialog('open'); }); } 

New

 function showHistory() { myId= $("#id").val(); //alert(myId); actionDialog.load("@Url.Action("ActionHistoryAjax", new {id = "+ myId +", sort = "abc"})", function () { actionDialog.dialog('open'); }); } 
-1
source

Source: https://habr.com/ru/post/946175/


All Articles