ASP.NET MVC session lost between page loads -


i trying use session state in mvc , have been stuck entire evening! realise session state should used sparingly in mvc - sure want use 1 page - appreciate solutions rather opinions.

basically, have contact form captcha image. when page loads want set session characters used in captch image (dynamically generated image). have 'new image' link async creates new code, sets session , dynamically loads new image screen.

the session stays sets long page doesn't load or reload. need able validate user input against code in session (which should reflect displayed user) session empty.

if perform ajax reload on image i.e. set session asynchronously - session set when perform post!!

what's going on?

i need able persist session value - arrrhhhh!

i have base controller:

public new httpcontextbase httpcontext {       {       httpcontextwrapper context = new httpcontextwrapper(system.web.httpcontext.current);       return (httpcontextbase)context;    }  }  

and in controller have:

  [acceptverbs(httpverbs.post)]   public actionresult contact(contactform c, string button)   {     string sesscaptcha = httpcontext.session["captcha_contact"] == null ? "" : httpcontext.session["captcha_contact"].tostring();   } 

any ideas????? pretty pls cherry on top :)

thanks!

i embarrased right now...

never ever set session value before response.end() (and believe same applies response.redirect).

omg - that's 4 hours never ever again!

here demo code illustrate ineptitude...

   public actionresult page1()         {             session["test1"] = "hello world";              // means session won't set             // it's used in valid circumstances..e.g. setting session, dynamically generating image based on value.             response.end();              return view();         }          public actionresult page2()         {             viewbag.test = session["test1"].tostring();              return view();         }          [httppost]         public actionresult page2(formcollection fc)         {             viewbag.test = "...is still..." + session["test1"].tostring();              return view();         } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -