3D-kreativ
3D-kreativ

Reputation: 9309

Increase value of session variable for each call to controller

Each time a click on a link in the View and call an ActionResult method in the Controller, I need to increase the value of the session variable, but it's not working.

When I start the value is 17 and when I click on the link and pass 1 to the Controller I want the result to be 18 and next time I click it should be 19.

But what have I done wrong and how can I improve the code? The value is a nullable int in the ActionResult.

public ActionResult Index(int? value) {

        // Session
        if (Session["week"] == null)
        {
            // Create session
            Session["week"] = week.WeekNum();
        }

        if (!value.HasValue)
        {
            weekStart = (int)Session["week"];
        }
        else
        {
            Session["week"] = + value;
            weekStart = (int)Session["week"];
        }

        ViewBag.weekNumber = weekStart;

        ... the rest of the code...
        }

Upvotes: 1

Views: 913

Answers (2)

Jason Evans
Jason Evans

Reputation: 29186

I suggest writing a helper function to do the increment e.g.

private void IncrementWeeks(int weeks)
{
    if (Session["week"] == null)
    {
        Session["week"] = week.WeekNum();
        return;
    }

    int currentWeek = 0;

    if (int.TryParse(Session["week"].ToString(), out currentWeek))
    {
        Session["week"] = (currentWeek + weeks).ToString();
    }
}

Just to cover NULL checking and int conversion issues.

Here's how you could wrap getting the value from the session into a helper function too:

private int ReadWeekFromSession()
{
    if (Session["week"] == null)
    {
        return 0;
    }

    int currentWeek = 0;

    if (int.TryParse(Session["week"].ToString(), out currentWeek))
    {
    }

    return currentWeek;
}

For example:

public ActionResult Index(int? value)
{
    int weekStart = 0;

    if (!value.HasValue)
    {
        weekStart = ReadWeekFromSession();
    }
    else
    {
        IncrementWeeks(value.Value);
        weekStart = ReadWeekFromSession();
    }

    // Rest of controller code....    
}

Upvotes: 1

David
David

Reputation: 219027

You'd increment the value just like you would from any other data source:

  1. Retrieve the value
  2. Modify the value
  3. Store the new value

Which could be something as simple as:

var weekValue = int.Parse(Session["week"].ToString());
weekValue += value;
Session["week"] = weekValue;

If it's possible that the session variable isn't a valid integer, you might use int.TryParse() instead or some other means of checking for that condition.

Or to avoid using that .ToString() call, if the potential for errors with the value is small enough to use int.Parse() in the first place then I suppose you could get away with this as well:

var weekValue = Convert.ToInt32(Session["week"]);

Which is a bit more forgiving on the input types, since it has many overloads.

Upvotes: 1

Related Questions