7

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

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".

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');
});
}
1

3 Answers 3

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');
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.

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.

Comments

-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');
 });
}

2 Comments

That won't work. That would attempt to assign the string value "+ myId +" to id
This didn't work for me. I literally got the text "myId" as the value for id.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.