tags:

views:

116

answers:

3

Hello, I'm creating a web based questionnaire in MVC where the questions are generated from a CSV file. The format is one question per page. I don't want to have to parse the CSV file every time the user goes to the next question but parse it once at the beginning of the questionnaire. At the moment the CSV file is parsed in the Controller's constructor and there is a method for returning a view with the next/previous question.

What is the best way of going about this so that the CSV file only has to be parsed once?

I'm relatively new to the production of dynamic websites so any help would be greatly appreciated.

Thanks

A: 

I dont think , you can persist all the questions in the controller's constructor .

Even if you are trying to use a static Dictionary datastructure to have all the questions in place , keyed by the question number. The controller is Disposed once the ActionResult is returned .

try having a breakpoint in the controller's constructor , and it gets hit each time you click on the prev/next link. Which means , the controller is instantiated each time a Http request is made.

vijaysylvester
that doesn't really answer the question
harryovers
by saying that , i mean the CSV cannot be parsed in one go and persisted for all the questions.
vijaysylvester
that was the point of the question, i don't think that you read it properly
harryovers
+1  A: 

At the begining: you should

  1. parse CSV file
  2. Create List of question ex: List<Question>
  3. Save the collection to Session object

Then when user answers the certain question you should simply

  1. Get Question object from Session
  2. Fill the answer ex questions[x].Answer = 1;
  3. Save back to Session

At the end you can iterate through collection of answered question and save answers to database etc.

dario
A: 

Parse the csv file and store it in a database.

BenB