简体   繁体   中英

Calling a javascript variable inside a c# code

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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