views:

48

answers:

1

Hi, I want to change an ASP.NET variable when someone clicks on a link on the page. Specifically a Session["something"] variable. That means I should do it after postback, but I am new to whole web development and the ASP.NET MVC specifically, so I know just an overloaded action with some [Accept] attribute. Since this link is on the master site, I can imagine only to add such an action to each action existing and that seems pretty unfortunate. Javascript doesn't have access to ASP.NET variables, so I am pretty lost.

This action link should be for almost every action and I don't know, how to recognize, whether it has been accesses by this spoecific or some other link (which is important to me). That implies some postprocessing, something like


<a href="" onclick="<% Session["location"] = new_value %>" >

Any ideas? Piece of code?

Thanks in advance.

+1  A: 

Just remember that asp code gets executed on the server and any output from that code is added to the HTML then the HTML is sent to the browser where the javascript runs. So you need to make a trip back to the server to change the session variable.

The code for the link,

<%= Html.ActionLink("MyLink", "MyAction", "MyController")%>

Then for the MyController,

public ActionResult MyAction()
{
    Session["something"] = "New Value";

    return View();
}
Tony Borf
unfortunatelly, this action link should be for almost every action and I don't know, how to recognize, whether it has been accesses this spoecific or some other link (which is important to me).
Trimack
You can add parameters to the link. <%= Html.ActionLink("MyLink", "MyAction", "MyController",new { id ='something' })%>
Tony Borf