1

Possible Duplicate:
How to pass variable from jquery to code in c#

In my View I have a variable named as shown below x:

@{ int x;}

I want to set a value to x from within my jQuery code.

How can I do it ?

1
  • Please explain your question in more detail. Commented Sep 25, 2011 at 8:46

3 Answers 3

1

by this:

@{var x = 5;}

<script type="text/javascript">
    var c= @(x);
    alert(c);
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Sounds like he's after the other way around.
Thanks for your fast answer but I want to do the opposite, @(x)=5
OK, js is a client-code and will run just on browsers. So you can use the link @Darin commented below the Q. Regards.
0

Just use @x to get the value. Ex.

@{int x = 42;}

<script type="text/javascript"> var x = @(x); alert('The answer to the Ultimate Question of Life, the Universe, and Everything is ' + x); </script>

1 Comment

Sorry on my poor English, in my script i need to change the x variable in order to use it in ActionLink like this <li>@Html.ActionLink("שיחות", "ClientTalksCheck", "Talk", new { id =x }, null)</li>
0

you can do something like this...

@{  // declare this in your view 
    int x  = 100;
}

    $(document).ready(function() {

    var postdata = @x + 100; // here is the updated code. if you want to change the value eg. this will alert 200
    alert(postdata);
    $.ajax({
            url: 'Home/Index',
            data: postdata,
            success: function (returnData) {
                alert(returnData); // this will alert 210
            }
        });
});

Your Controller

public ActionResult Index(int x) // x will get the value here which is 200 here now..
{
   var y = x + 10; // do some logic here with the posted data from your view
   return Json(y,JsonRequestBehaviour.AllowGet); // you will need to return Json here cos you using the AJAX call
}

2 Comments

i need to change the value of x,@(x)=100 but its not working
you mean that you want to change the value of x in Jquery code? like var postdata = @(x) + 200 // example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.