Tuesday 15 April 2014

c# - Create List only if Session doesn't exist -


target

If no session is present then create only one list.

Problem

The Visual Studio gives me:

The name products is not present in the current context.

I have the following code:

  Add public action Add (int productId) {if (session ["shopping list"] == zero) {list & Lt; Int & gt; Products = new list & lt; Int & gt; (); } Products.Add (productId); Session ["shopping list"] = product; See Return ("Index"); }   

Yes, I know, I have not set the if to the products variable. But, if the session is present, So this means that the "list" already exists and theoretically products already exist.

So, how can I fix this?

More short code for which you should reassign session ["shopping list"] Do not require each time:

  add public action (add int id) {if (session ["shopping list"] == empty) {Session ["shopping list" ] = New list & lt; Int & gt; (); } Add ([list & lt; int & gt; session] session ["shopping list"]) (product ID); See Return ("Index"); }    

No comments:

Post a Comment