MVC单选按钮要回发到Controller的内容 [英] MVC RadioButtonFor How & What to POST back to Controller

查看:88
本文介绍了MVC单选按钮要回发到Controller的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#和MVC的新功能,所以提前道歉以发布可能很明显的内容.我已经看过类似的答案,但仍然看不到应该如何使用RadioButtonFor中的值以及应该使用什么值以便可以过帐回到控制器.

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.

控制器

    [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"); 

和视图

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

我还尝试了将字符串从CorrectAnswer传递到成绩控制器而没有成功.非常感谢您指出如何将选中的RadioButton值传递回控制器?

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?

推荐答案

您不应具有3个不同的属性,而应具有一个包含答案的属性.这将使您能够对单选按钮进行分组,并只能选择其中一个:

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

,然后在控制器操作中检查发送的Answer属性值对于该问题是否正确.从视图中可以看到,我们对该问题有多个答案,其值就是将被发送到服务器的内容.通常,您将使用答案的ID作为值,并且可以在服务器上比较这是否是该问题的正确答案.

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.

这篇关于MVC单选按钮要回发到Controller的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆