可能重复:
How to pass variable from jquery to code in c#如何将变量从jQuery传递给MVC3视图中的变量?
在我看来,我有一个名为变量如下图所示X:
@{ int x;}
我想从内部设定的值X我的jQuery代码。
我该怎么办?
可能重复:
How to pass variable from jquery to code in c#如何将变量从jQuery传递给MVC3视图中的变量?
在我看来,我有一个名为变量如下图所示X:
@{ int x;}
我想从内部设定的值X我的jQuery代码。
我该怎么办?
本:
@{var x = 5;}
<script type="text/javascript">
var c= @(x);
alert(c);
</script>
听起来好像他是以相反的方式。 –
感谢您的快速回答,但我想做相反的事情,@(x)= 5 – avi
好的,js是一个客户端代码,只能在浏览器上运行。因此,您可以使用链接@Darin在Q.问候下面评论。 –
只需使用@x
来获取值。防爆。
@{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>
对不起,我的英文不好,在我的脚本中,我需要更改x变量以便在ActionLink中使用它,像这样:
你可以做这样的事情...
@{ // 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
}
});
});
控制器
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
}
我需要更改x的值,@(x)= 100但它不起作用 – avi
您的意思是您想要更改jQuery代码中的x值? like var postdata = @(x)+ 200 //示例 –
看看我更新的代码... –
请更详细地解释一下你的问题。 –