1 Replies - 2512 Views - Last Post: 31 July 2013 - 06:44 AM Rate Topic: -----

#1 BattlFrog  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 72
  • Joined: 09-April 12

How to code a survey with questions from a database

Posted 29 July 2013 - 03:25 PM

Although I am using MVC, I am thinking this may be a broader questionn, so I put it here, insteead of the MVC sub topic.

I have about 300 questions in a db, Question(id, QuestionText). Using mvc, I am adding a survey to a web site, kind of a questionaire in the profile page setion of the site. This is proving to be more difficult than I had anticipated, so I am looking for help. I am trying to decide on a few things, and I was wondering if anyone knows of any sample projects online.

Some specific areas I am having trouble are:

Database design. I am thinking of something like:

Question(id, Text)
Answer_Type(id, type) (Types would be yes/no, numeric, text)
User_Answer(id, personid, questionid, answerTypeId, Answer)

I am just not sure about that last table, as it would mean all the answer data types would have to be varchars or something.



Second, How to display the questions on the View. I am going to have 10 questions per page, (or I may use partial pages).

My first thought is a foreach loop, but i tried that and it looks like all the questions on a page would have to be the same answer_type, because the loop would be:

@foreach(var item in Model)
{
   @Html.LabelFor(modelitem => item.QuestionText)
   @Html.RadioButtonFor(modelitem => item.Answer, "yes") Yes
   @Html.RadioButtonFor(modelitem => item.Answer, "no") no
}




Anywho, let me know if you know of any examples of how to do this, thanks.

This post has been edited by BattlFrog: 29 July 2013 - 04:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to code a survey with questions from a database

#2 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: How to code a survey with questions from a database

Posted 31 July 2013 - 06:44 AM

well if you have an anwer_type column you could add some if/else logic to the loop to display the correct controls for the given type

@foreach(var item in Model)
{
  if (item.AnswerType == "yes/no")
  {
    @Html.LabelFor(modelitem => item.QuestionText)
    @Html.RadioButtonFor(modelitem => item.Answer, "yes") Yes
    @Html.RadioButtonFor(modelitem => item.Answer, "no") no
  {
  else if (item.AnswerType == "text")
  {
    @Html.LabelFor(modelitem => item.QuestionText)
    @Html.TextBoxFor(modelitem => item.Answer)
  }
  else if.... // Keep going with whatever types
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1