MVC RadioButtonFor How & What to POST back to Controller

不问归期 提交于 2019-12-11 06:27:32

问题


New new to C# and MVC, so apologies in advance for posting something which is probably obvious.I have looked at similar answers but still can't see how and what value in the RadioButtonFor should be used so that it can be POSTed back to the controller.

Controller

    [HttpPost]
    public ActionResult Score(ExamViewModel exam)
    { 
        const int AddCorrect = 1;

        var correct = from c in db.Answers
                      where c.ID == 1
                      select c.CorrectAnswer;


        if (ModelState.IsValid)
        {
            if (correct == exam.CorrectAnswer) 
            {

                ViewData["message"] = "Correct Answer!";

                return View("Results");
           }

            else
            {
                var feedback = from g in db.Questions
                               where g.ID == 1
                               select g.GrammarPoint;

                ViewData["message"] = "That's not the right answer.";
                 ViewData["feedback"] = feedback;

                return View("Results");
            }

        }

        return View("Results"); 

And the View

  @model AccessEsol.Models.ExamViewModel

@{
    ViewBag.Title = "TakeTest";
}

<h2>TakeTest</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>


    <div class="display-label">
        <h3>Click the correct answer:</h3>
    </div>

    <div class="display-field">

        <strong>@Html.DisplayFor(model => model.Text.Text )</strong>
    </div>

    @Html.DisplayFor(model => model.Foil1.Foil1)
   @Html.RadioButtonFor(model =>model.Foil1, "Incorrect" ) 

    @Html.DisplayFor(model => model.Foil2.Foil2)   
    @Html.RadioButtonFor(model => model.Foil2, "Incorrect" )

   @Html.DisplayFor(model => model.Foil3.Foil3)
    @Html.RadioButtonFor(model => model.Foil3, "Incorrect")


    @Html.DisplayFor(model => model.CorrectAnswer.CorrectAnswer)
    @Html.RadioButtonFor(model => model.CorrectAnswer, "Correct")   

    <p>
        <input type="submit" value="Submit Answers" />
    </p>

</fieldset>
}

I also tried to pass a string from the CorrectAnswer into the Score Controller without success.Much appreciated if you can point to how can checked RadioButton value can be passed back to the Controller?


回答1:


You should not have 3 different properties but instead a single one that will contain the answer. This will allow you to group the radio buttons and be able to select only one of them:

@model AccessEsol.Models.ExamViewModel

@{
    ViewBag.Title = "TakeTest";
}

<h2>TakeTest</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <div class="display-label">
            <h3>Click the correct answer:</h3>
        </div>

        <div class="display-field">
            <strong>@Html.DisplayFor(model => model.Text.Text)</strong>
        </div>

        @Html.DisplayFor(model => model.Foil1.Foil1)
        @Html.RadioButtonFor(model => model.Answer, "1") 

        @Html.DisplayFor(model => model.Foil2.Foil2)   
        @Html.RadioButtonFor(model => model.Answer, "2")

        @Html.DisplayFor(model => model.Foil3.Foil3)
        @Html.RadioButtonFor(model => model.Answer, "3")

        @Html.DisplayFor(model => model.CorrectAnswer.CorrectAnswer)
        @Html.RadioButtonFor(model => model.Answer, "4")

        <p>
            <input type="submit" value="Submit Answers" />
        </p>
    </fieldset>
}

and then in your controller action check if the Answer property value that is sent is the correct one for this question. As you can see from the view we have multiple answer for the question and the value is what will get sent to the server. Usually you will use the ID of the answer as value and on the server you can compare whether this is the correct answer for the question.



来源:https://stackoverflow.com/questions/20886730/mvc-radiobuttonfor-how-what-to-post-back-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!