如何从jQuery的传递变量code在C# [英] How to pass variable from jquery to code in c#

查看:144
本文介绍了如何从jQuery的传递变量code在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网页我有一个int变量名mySerial,我想从脚本传递值

in my page i have an int variable name mySerial and i want to pass a value from a script

mySerial = ui.item.Serial不能正常工作

mySerial =ui.item.Serial is not working

推荐答案

您可以这个变量作为查询字符串参数传递给一些控制器动作:

You could pass this variable as query string parameter to some controller action:

<script type="text/javascript">
    var mySerial = '12345';
    $.ajax({
        url: '@Url.Action("Foo", "Home")',
        type: 'POST',
        data: { mySerial: mySerial },
        success: function(result) {
            alert('success');
        }
    });
</script>

动作:

[HttpPost]
public ActionResult Foo(string mySerial)
{
    ... do something with the serial here
}

另一种可能性是执行重定向,如果你不希望使用AJAX:

Another possibility is to perform a redirect if you don't want to use AJAX:

<script type="text/javascript">
    var mySerial = '12345';
    var fooUrl = '@Url.Action("Foo", "Home")';
    window.location.href = fooUrl + '?mySerial' + encodeURIComponent(mySerial);
</script>

或者,也许我误解你的问题,你想你的JavaScript变量分配给某个值从视图模型来吗?在这种情况下:

Or maybe I misunderstood your question and you want to assign your javascript variable to some value coming from the view model? In this case:

<script type="text/javascript">
    var mySerial = @Html.Raw(Json.Encode(Model.MySerial));
</script>

这篇关于如何从jQuery的传递变量code在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆