简体   繁体   English

我可以将ac#变量分配给javascript函数的return语句吗?

[英]Can I assign a c# variable to to the return statement of a javascript function?

Suppose I have a C# variable named currentTab in my razor view. 假设我在剃刀视图中有一个名为currentTab的C#变量。

I have javascript codes that says.. 我有说的JavaScript代码..

<script type="text/javascipt">
    function CurrentTabReturner(){
        var currTab = $("a.current").attr("id");
        return currTab;
    }
</script>

Now I want to do this: 现在我要这样做:

@currentTab = CurrentTabReturner();

Is it possible? 可能吗? Or are there any other ways to achieve this? 还是有其他方法可以实现这一目标?

Thanks in advance. 提前致谢。

Is it possible? 可能吗?

No it is not possible. 不,这是不可能的。 Server side variables exist on the server. 服务器端变量存在于服务器上。 Client side javascript runs inside the client browser much later, where no more any server variables exist. 客户端javascript在客户端浏览器中运行的时间要晚得多,在该浏览器中不再存在任何服务器变量。 So trying to assign a server side variable to a client side function simply doesn't make sense. 因此,尝试将服务器端变量分配给客户端函数根本没有任何意义。 On the client you could send normal or asynchronous (AJAX) requests to the server so that server side variables can be updated. 在客户端上,您可以向服务器发送普通或异步(AJAX)请求,以便可以更新服务器端变量。

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

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