Lamloumi Afif
Lamloumi Afif

Reputation: 9081

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?

Upvotes: 0

Views: 129

Answers (1)

geevee
geevee

Reputation: 5451

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.

Upvotes: 1

Related Questions