2014-12-30 8 views
5

Sto provando a passare il valore del pulsante di scelta selezionato dalla vista del rasoio al controller .... qualcuno può darmi un'idea su come fare questo? ..................... My View si presenta così:Passando il valore selezionato dai pulsanti di opzione al controller in MVC

@using (Html.BeginForm("Index", "Exam")) 
{ 

    @Html.Hidden("qid", Model.ID, new { @id = "id" }) 
    <table> 
     <tr> 
      <td> 
       @Model.ID 
      </td> 
      <td> 
       @Model.QuestionDes 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <p>@Html.RadioButton("Answer1", new { @id = 1 }) @Model.Answer1 </p> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <p>@Html.RadioButton("Answer2", new { @id = 2 }) @Model.Answer2 </p> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <p>@Html.RadioButton("Answer3", new { @id = 3 }) @Model.Answer3 </p> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <p>@Html.RadioButton("Answer4", new { @id = 4 }) @Model.Answer4 </p> 
      </td> 
     </tr> 

    </table> 

    <input type="submit" value="Next" /> 

} 
@using (Html.BeginForm("PrevIndex", "Exam")) 
{ 

    @Html.Hidden("qid1", Model.ID, new { @id = "id1" }) 
    <input value="Prev" type="submit" /> 
} 

mio controller simile a questa: .........

public class ExamController : Controller 
    { 
     [HttpGet] 
     public ActionResult Index() 
     { 
      IQuestionService ser = new QuestionService(); 
      QuestionLoadDTO q = ser.GetIndividualQuestions(1); 
      return View(q); 
     } 

     [HttpPost] 
     public ActionResult Index(QuestionLoadDTO ques) 
     { 
      int count = 0; 
      count = int.Parse(Request["qid"].ToString()); 
      count++; 
      if (count <= 4) 
      { 
       IQuestionService ser = new QuestionService(); 
       QuestionLoadDTO q = ser.GetIndividualQuestions(count); 
       return View(q); 
      } 
      return RedirectToAction("Submit"); 

     } 
     public ActionResult PrevIndex(QuestionLoadDTO ques) 
     { 
      int count1 = 0; 
      count1 = int.Parse(Request["qid1"].ToString()); 
      count1--; 
      if (count1 < 5 || count1 >= 0) 
      { 
       IQuestionService ser = new QuestionService(); 
       QuestionLoadDTO q = ser.GetIndividualQuestions(count1); 
       return View("Index", q); 
      } 
      return RedirectToAction("End"); 

     } 
     public ActionResult Submit() 
     { 
      return View(); 
     } 
     public ActionResult End() 
     { 
      return View(); 
     } 




    } 

Questi sono gli altri metodi:

QuestionLoadDTO IQuestionService.GetIndividualQuestions(int index) 
{ 
    IQuestionData ser = new QuestionRepository();   

    QuestionLoadDTO q = ser.GetIndividualQues(index); 

    return q; 
} 
public QuestionLoadDTO GetIndividualQues(int index) 
{ 
    Context con = new Context(); 
    Question question = con.Questions.Find(index);//Where(e => e.ID == index).FirstOrDefault(); 
    QuestionLoadDTO dto = new QuestionLoadDTO() 
    { 
     ID = question.ID, 
     QuestionDes = question.QuestionDes, 
     Answer1 = question.Answer1, 
     Answer2 = question.Answer2, 
     Answer3 = question.Answer3, 
     Answer4 = question.Answer4 

    };         
    return dto; 
} 

Grazie !!!!

+0

Effettua fatture nascoste per ogni domanda. – Mairaj

risposta

10

Aggiungi proprietà:

public string SelectedAnswer { get; set; } 

Aggiungi in vista:

@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer1") 
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer2") 
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer3") 
@Html.RadioButtonFor(m => m.SelectedAnswer, "Answer4") 

controller si postback il valore in base al pulsante di scelta selezionato. .. ovvero Risposta1 o Risposta2, ecc.

1

O fare un campo nascosto per ogni pulsante di scelta o cambiamento che i pulsanti di opzione come questo

@Html.RadioButtonFor("Answer1", new { @id = 1 }) 
+0

ahhh ok ci proverò ... – Dayan

4

Se si stanno dando stesso nome per i tuoi pulsanti di opzione, allora si può provare il seguente codice

controllore

[HttpPost] 
public ActionResult Index(string Answer) 
{ 
    return View(); 
} 

View

@using (Html.BeginForm("Index", "Demo")) 
{ 
     @Html.RadioButton("Answer", "A") <span>A</span> 
     @Html.RadioButton("Answer", "B") <span>B</span> 

    <input type="submit" value="Next" /> 
} 
1

collezione Utilizza il modulo

[HttpPost] 
    public ActionResult Index(FormCollection fc) 
    { 

     string answerA = fc["Answer1"]; 
     string answerB = fc["Answer2"]; 
     string answerC = fc["Answer3"]; 
     string answerD = fc["Answer4"]; 
     return View(); 
    } 
Problemi correlati