I'm an old hand at .Net programming, but I'm new to this whole ASP.NET thing, so these are really beginner questions...
My first ever ASP.NET app is a questionaire. My data model basically consists of a list of questions. Each question and its answer entry form are displayed to the user one-question-at-a-time. The page has a "Next" button to go to the next question.
When the user clicks the Next button, the POST controller needs to increment a persistent variable that contains the current question, then display a view containing the new question.
Ok, so here are some questions regarding this scenario:
1. Where should I stash the question index? As I understand it, my choices are either in the database (I'm using Entity Framework) or in a session variable (this.HttpContext.Session["QuestionIndex"]). Is there any rule-of-thumb that says one is preferred
over the other?
2. If the user clicks the Next button twice before the server has a chance to send new content to the browser then I assume I'll end up with two (possibly simultaneous) instances of my POST controller. (I realize that I could learn enough basic Javascript
to disable the button after the first click, but I'd rather harden the controller against this scenario.) Is there an easy way to recognize and ignore second or subsequent posts? If I can't easily "throw away" extra button clicks then what happens when each
of the two controllers eventually calls its View routine? Does the browser see one and not the other? Or does the browser somehow wind up displaying whichever View is displayed last?
1. Where should I stash the question index? As I understand it, my choices are either in the database (I'm using Entity Framework) or in a session variable (this.HttpContext.Session["QuestionIndex"]). Is there any rule-of-thumb that says one is preferred
over the other?
Why not pass it as a param with the POST? You can have a <input type=hidden> anf his is perfect for passing extra per-request data.
bob.at.sbs
2. If the user clicks the Next button twice before the server has a chance to send new content to the browser then I assume I'll end up with two (possibly simultaneous) instances of my POST controller. (I realize that I could learn enough basic Javascript
to disable the button after the first click, but I'd rather harden the controller against this scenario.) Is there an easy way to recognize and ignore second or subsequent posts? If I can't easily "throw away" extra button clicks then what happens when each
of the two controllers eventually calls its View routine? Does the browser see one and not the other? Or does the browser somehow wind up displaying whichever View is displayed last?
Yea, common problem. Usually ihding ro disabling the button until the next page comes up is the easiest answer. The more robust (yet more work) approach would be to track in your DB is this user has already submitted page 3 (or whatever) and then prompt
them if this is what they wanted to do (or just ignore the POST).
bob.at.sbs
Member
1 Points
4 Posts
Newby questions re: session state
Aug 07, 2012 09:38 PM|LINK
I'm an old hand at .Net programming, but I'm new to this whole ASP.NET thing, so these are really beginner questions...
My first ever ASP.NET app is a questionaire. My data model basically consists of a list of questions. Each question and its answer entry form are displayed to the user one-question-at-a-time. The page has a "Next" button to go to the next question.
When the user clicks the Next button, the POST controller needs to increment a persistent variable that contains the current question, then display a view containing the new question.
Ok, so here are some questions regarding this scenario:
1. Where should I stash the question index? As I understand it, my choices are either in the database (I'm using Entity Framework) or in a session variable (this.HttpContext.Session["QuestionIndex"]). Is there any rule-of-thumb that says one is preferred over the other?
2. If the user clicks the Next button twice before the server has a chance to send new content to the browser then I assume I'll end up with two (possibly simultaneous) instances of my POST controller. (I realize that I could learn enough basic Javascript to disable the button after the first click, but I'd rather harden the controller against this scenario.) Is there an easy way to recognize and ignore second or subsequent posts? If I can't easily "throw away" extra button clicks then what happens when each of the two controllers eventually calls its View routine? Does the browser see one and not the other? Or does the browser somehow wind up displaying whichever View is displayed last?
BrockAllen
All-Star
27438 Points
4893 Posts
MVP
Re: Newby questions session state
Aug 07, 2012 09:47 PM|LINK
Why not pass it as a param with the POST? You can have a <input type=hidden> anf his is perfect for passing extra per-request data.
Yea, common problem. Usually ihding ro disabling the button until the next page comes up is the easiest answer. The more robust (yet more work) approach would be to track in your DB is this user has already submitted page 3 (or whatever) and then prompt them if this is what they wanted to do (or just ignore the POST).
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/