问题
I need to set the value of a session with jquery. im thinking i need it to be done on server side but how do i do that with jquery? i got a link thats gone trigger the whole thing and my code so far is this.
<a href="#" id="showCart">Show Cart</a>
$('#showCart').click(function() {
$('#cartContainer').show();
//need to the set a session for the site to know if carts gone be visible
});
回答1:
For invoking something in the server side, you shall have to invoke a web method or a web page (aspx) from your jQuery method. You may have to pass on the required data as parameter(s). The task of setting the value to the session will be done from the server side code.
回答2:
You'll need to create a handler on the server that sets the session - in ASP.NET MVC, you simply create an action method that takes whatever data you need to set the session variable, and sets it. You then call the action method via jQuery's AJAX API.
A better option though, is to set a cookie instead. That will be available on both sides, and can be set directly on the client. Read more...
回答3:
This is what i did
$('#showCart').click(function() {
$('#cartContainer').show();
$.post('/Home/SaveSession');
});
[WebMethod]
public void SaveSession()
{
Session["IsCartVisible"] = "true";
}
来源:https://stackoverflow.com/questions/6729538/how-do-i-set-the-value-on-a-session-with-jquery