web-dev-qa-db-fra.com

Passer la valeur sélectionnée des boutons radio au contrôleur dans MVC

J'essaie de transmettre la valeur de ce bouton radio sélectionnée de la vue rasoir au contrôleur .... ........ Ma vue ressemble à ceci:

@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" />
}

Mon contrôleur ressemble à ceci: .........

 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();
        }




    }

Ce sont les autres méthodes:

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;
}

Je vous remercie!!!!

5
Dayan

Ajouter une propriété:

public string SelectedAnswer { get; set; }

Ajouter en vue:

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

Dans le contrôleur, il poste la valeur en fonction du bouton radio sélectionné ... c’est-à-dire Answer1, ou Answer2, etc.

13
Mahesh

Si vous donnez le même nom à vos boutons radio, vous pouvez essayer le code suivant

Manette

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

Vue

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

    <input type="submit" value="Next" />
}
4
Golda

Créez un champ caché pour chaque bouton radio ou changez-le comme ceci

@Html.RadioButtonFor("Answer1", new { @id = 1 })
2
Mairaj

Utiliser la collection de formulaires 

[HttpPost]
    public ActionResult Index(FormCollection fc)
    {

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