Calling a javascript variable inside a c# code

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have this Javascript function in my asp.net mvc4 with razor application

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

<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".

How can i fix this problem?

回答1:

you just can't.

you can set values in javascript coming from your server side, but you can't do it the other way.

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.

something like:

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

hope that helps.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!