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 ?
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 ?
by this:
@{var x = 5;}
<script type="text/javascript">
var c= @(x);
alert(c);
</script>
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>
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
}