简体   繁体   English

我们如何从JavaScript向C#方法传递变量

[英]How we pass variable to c# method from JavaScript

I want to pass variable to C# method from JavaScript is it possible ? 我想将变量从JavaScript传递给C#方法,这可能吗?

I had tried below code code:= 我试过下面的代码:=

<script type="text/javascript">
    $(document).ready(function () {
        debugger;
        var Query = window.location.search;
        var i = "<%=QueryStringModule.Decrypt(Query)%>"
    });
</script>

but i am getting the name "Query" doesn't exist in the current context 但我得到的名称“查询”在当前上下文中不存在

Please help me in the same 请同样帮我

This is not possible. 这是不可能的。 C# is code behind, so runs on the server. C#是后面的代码,因此可以在服务器上运行。 Any input is generated before the page is build on screen. 在屏幕上构建页面之前,将生成任何输入。 So passing values from Javascript to C# is the other way around. 因此,将值从Javascript传递到C#是另一种方法。 Look into ajax to send values to the server so they can be parsed. 查看ajax将值发送到服务器,以便可以对其进行解析。

You cannot pass JavaScript variables to the server side code in that way. 您不能以这种方式将JavaScript变量传递给服务器端代码。 JavaScript is client side, which means that the server side code has already run before JavaScript starts executing. JavaScript是客户端,这意味着服务器端代码已经在JavaScript开始执行之前运行。 To pass a variable back to the server, you will need to submit the variable to the server. 要将变量传递回服务器,您需要将变量提交给服务器。 You can use an AJAX request to do this without having to load the page again. 您可以使用AJAX请求来执行此操作,而不必再次加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM