简体   繁体   English

在AC#代码中调用JavaScript变量

[英]Calling a javascript variable inside a c# code

I have this Javascript function in my asp.net mvc4 with razor application 我在带有剃刀应用程序的asp.net mvc4中具有此Javascript函数

function openbox2(formtitle, fadin) {
         var self = $(this);
         var arr = self.data('arr');
         @{
         Session["element"] = @:arr;

                             }
         var box = document.getElementById('box');
         document.getElementById('shadowing').style.display = 'block';

         var btitle = document.getElementById('boxtitle');
         btitle.innerHTML = formtitle;

         if (fadin) {
             gradient("box", 0);
             fadein("box");
         }
         else {
             box.style.display = 'block';
         }
     }

html part code html零件代码

<td> 
         <a href="#" onClick="openbox2('Validation de concept technique', 1)" data-arr="@fa.Id_element">Donner votre avis</a>. 
        </td>

My problem is that the instruction Session["element"] = @:arr didn't work even i replace it by Session["element"] = "@:arr" . 我的问题是,即使我用Session["element"] = "@:arr"代替它,指令Session["element"] = @:arr也不起作用。

How can i fix this problem? 我该如何解决这个问题?

you just can't. 你就是不能。

you can set values in javascript coming from your server side, but you can't do it the other way. 可以使用来自服务器端的javascript设置值,但不能采用其他方法。

what you can do, is to send an ajax request on page load with this array, so it will be saved in the Session variable on server side. 您可以做的是,使用此数组在页面加载时发送一个ajax请求,以便将其保存在服务器端的Session变量中。

something like: 就像是:

$(function(){
    $.post('/saveArray', {items:arr});
});

hope that helps. 希望能有所帮助。

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

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