0

I am pretty new to ASP MVC and faced problem while implementing the partial view with submit form\Ajax.

My Partial View :

@model IList<QuestionModel>

<label>@model.Question</label>

<form action="/ResponseGather/GetAnswers" method="POST"> //Question#1
    <input type="radio" name="answer" value="A"/> @model.AnswerOptionA 
    <input type="radio" name="answer" value="B"> @model.AnswerOptionB
<input type="submit">

My Question Model :

public class Question{
     public int Id {get;set;}
     public int NumberInSystem {get;set;}
     public string Question {get;set;}
     public string AnswerOptionA {get;set;}
     public string AnswerOptionB {get;set;}
}

In the repository there are defined 20 questions.

My ResponseGatherController:

public class ResonseGatherController : Controller {
     public void GetAnswers(AnswerModel model){
     //WHAT to do here?
     //I need gather answers to all 20 questions and then send it to DB.
}

And finally MyAnswerModel:

public string UserName {get;set;}
public string Email {get;set;}
public List<char>answers {get;set;}

I think that need following algorithm :

  1. Send to Partial View Question#1
  2. Wait User to Submit
  3. By using Ajax/Json/Whatever is used in this situation send next question to the partial without reloading the page.
  4. Save Question#1 answer somewhere and the same for all 20 questions.
  5. Only when all 20 questions answered, then send AnswerModel to DB by EF.
  6. ...Logic to analyze AnswerModel...

Could you advice me what to do? Should I use Json here? Or should I send an array of questions to the method?

Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35
  • 1
    Your algorithm is fine. :) I'm just wondering if you really need to wait for the user to answer all of the questions before saving anything to the database? Saving every time might be simpler. – OJ Raqueño Apr 06 '16 at 09:54
  • Just generate all 20 questions in the view (if you really want to hide then, then use javascript to display the appropriate one based on next and back buttons) and post them back and save. And you models don't make sense in relation to view you want so need to be redesigned. [This answer](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) might be more than you need, but should help you understand how to design the models and the view. –  Apr 06 '16 at 10:27
  • OJRaqueno, the reason I want to save results only after all questions answered is the fact, that section#6 (Logic to analyze AnswerModel) must get all answers before it can procedure output based on user's input. – Iskander Raimbaev Apr 06 '16 at 13:23

0 Answers0