将变量作为参数传递给javascript中的Url.Action

时间:2021-11-20 15:12:06

I am passing parameters to @Url.Action like this:

我将参数传递给@ Url.Action,如下所示:

function showHistory()
{

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

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

但是给出错误“myId在当前上下文中不存在”。

How i can pass the variable?

我怎么能传递变量?

I solved this, this is the 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');
});
}

3 个解决方案

#1


8  

I solved it by using this:

我用这个来解决它:

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

#2


2  

You're mixing server-side and client-side code here. You can't pass a Javascript variable into the Url.Action method like that.

您在这里混合服务器端和客户端代码。你不能像这样将Javascript变量传递给Url.Action方法。

You would 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 else load that ID from the server model.

或者从服务器模型加载该ID。

#3


-1  

old one
function showHistory()
{

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

New one

function showHistory()
{

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

#1


8  

I solved it by using this:

我用这个来解决它:

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

#2


2  

You're mixing server-side and client-side code here. You can't pass a Javascript variable into the Url.Action method like that.

您在这里混合服务器端和客户端代码。你不能像这样将Javascript变量传递给Url.Action方法。

You would 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 else load that ID from the server model.

或者从服务器模型加载该ID。

#3


-1  

old one
function showHistory()
{

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

New one

function showHistory()
{

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