如何在MVC视图页面中使用javascript变量值初始化C#变量 [英] How to intialize C# variable with javascript variable value in MVC view page
问题描述
>我有
@
{
string Map = string.Empty;
}
var nonID = 1;
@ Map = nonID
& lt; / script
< b>我尝试过:< / b>
i已经尝试了
< pre>我有
@
{
string Map = string.Empty;
}
& lt; script>
var nonID = 1;
@ Map = nonID
& lt; / script< / pre>
但不工作
当 F-ES Sitecore 写道时,JavaScript代码运行(在客户端中)只有在运行.NET代码(在服务器端)并且已经从中创建了HTML文档之后,浏览器才显示HTML文档。在服务器端呈现页面时,不会运行JavaScript代码(它只是复制到创建的HTML文档的文本)。所以它只能编写一个简单的赋值(就像你尝试过的那样)是行不通的。
但是,如果你想要一种机制来根据改变服务器的.NET对象值客户端的JavaScript对象值,您可以尝试我的 WebBinding
>i have
@
{
string Map = string.Empty;
}
var nonID = 1;
@Map=nonID
</script
<b>What I have tried:</b>
i have tried
<pre>i have
@
{
string Map = string.Empty;
}
<script>
var nonID = 1;
@Map=nonID
</script</pre>
but not working
As F-ES Sitecore wrote, the JavaScript code is run (in the client side when the browser shows the HTML document) only after the .NET code has been run (in the server side) and, an HTML document has been created from it. The JavaScript code isn't run when rendering the page in the server side (it just a text that is copied to the created HTML document). So it can't work by just writing a simple assignment (like you tried).
But, if you want a mechanism for changing a server's .NET objects values according to client's JavaScript objects values, you can try my WebBinding.
这篇关于如何在MVC视图页面中使用javascript变量值初始化C#变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!