Click here to Skip to main content
15,913,234 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i click tree view node am getting id by giving alert

JavaScript
$('.treeview a').on('click', function () {
alert(this.id);
});


What I have tried:

but how to save that id session , i want to use in controller
i have tried this
JavaScript
$('.treeview a').on('click', function () {
@Session["id"]=this.id;
});

but am not getting value
Posted
Updated 20-Jul-16 22:50pm
Comments
Karthik_Mahalingam 21-Jul-16 9:56am    
use jquery ajax calls to get/set value from/to session.

Please try like this I created function that create cookie.
JavaScript
function deleteCookie(key) {
       document.cookie = key + '="";expires=Thu, 01 Jan 1970 00:00:01 GMT;';
   };
   function setCookie(key, value) {
       var expires = new Date();
       expires.setTime(expires.getTime() + (60 * 1000));
       document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
   }

set cookie in click function.
JavaScript
$('.treeview a').on('click', function () {
   setCookie('id', this.id);
});
 
Share this answer
 
Comments
Member 11382784 21-Jul-16 2:58am    
thank you for responding sir, how to use that in controller
Dil0500 21-Jul-16 4:48am    
create ajax to pass id value to controller and create session from there.
 
Share this answer
 
You'll need to call an action via jquery and pass the id to the action and the action will update the session for you, there is a code example in the link below. You have to remember that your .net code isn't "running inside" the browser so your razor code isn't executed by the browser, it is executed by .net on the server and the resulting static html is sent to the browser. View the page source to see what is actually being executed and you'll see your @Session code is nowhere to be seen.

asp.net mvc 3 - How to Create Session Variable in JavaScript MVC3 Razor View engine .cshtml - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900