【问题标题】:How can i pass multiple radio button values to controller in ASP.NET MVC?如何将多个单选按钮值传递给 ASP.NET MVC 中的控制器?
【发布时间】:2020-09-02 06:09:02
【问题描述】:

我的模型中包含 3 个表。

public class InExam
{
    public AutoTests TheTest { get; set; }
    public List<InTest> TheQuestions { get; set; }
    public IEnumerable<Result> SingleQuee { get; set; }
}

第一个获取详细页面,例如“admin/AutoTests/id”

第二次获得链接到页面的问题列表

第三个是保存单选按钮字符串以将其发布回控制器

我的计划是获取(比如说)20 个与详细页面链接的问题,为每个问题添加 4 个单选按钮,并将每个选定的按钮发回控制器。

我的视图形式:

    @using (Html.BeginForm("Test", "Exams", new { id = Model.TheTest.id }, FormMethod.Post))
{
    foreach (var item in Model.TheQuestions)
    {
        Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x => x.Question == item.Question).FirstOrDefault();
        <div class="container" style="padding-top:50px;direction:rtl;">
            <h4 style="text-align:right;font-weight:bold;">@item.Question</h4>
            <div class="container">
                <div class="row" style="direction:rtl;">
                    <div class="col-lg-7" style="text-align:right;margin-right:10px;">
                        <div class="row">
                            @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "1" })
                            <h5 style="padding-top:3px;padding-right:8px;">@item.RightAnswer</h5>
                        </div>
                    </div>
                    <div class="col-lg-7" style="text-align:right;margin-right:10px;">
                        <div class="row">
                            @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "2" })
                            <h5 style="padding-top:3px;padding-right:8px;">@item.Answer2</h5>
                        </div>
                    </div>
                    <div class="col-lg-7" style="text-align:right;margin-right:10px;">
                        <div class="row">
                            @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "3" })
                            <h5 style="padding-top:3px;padding-right:8px;">@item.Answer3</h5>
                        </div>
                    </div>
                    <div class="col-lg-7" style="text-align:right;margin-right:10px;">
                        <div class="row">
                            @Html.RadioButtonFor(x => singleQuee.Question, new { @class = "form-control dot", @Name = singleQuee.Question, @Value = "4" })
                            <h5 style="padding-top:3px;padding-right:8px;">@item.Answer4</h5>
                        </div>
                    </div>
                    @Html.HiddenFor(m => singleQuee.Question)
                </div>
            </div>
        </div>
    }


                <button class="btn botton" type="submit" onclick="return confirm('');">END</button>

}

在我看来,我使用了这一行“Kafo.Models.Result singleQuee = Model.SingleQuee.Where(x =&gt; x.Question == item.Question).FirstOrDefault();”,因为我不能使用元组 foreach(C# 版本 5)

这是我的控制器代码:

[HttpGet]public ActionResult Test(int? id)
    {
        using (KafoEntities db = new KafoEntities())
        {
            InExam model = new InExam();
            model.TheTest = db.AutoTests.Where(x => x.id == id).FirstOrDefault();

            model.TheQuestions = db.InTest.Where(x => x.UserEmail == currentUser.Email && x.ExamId == model.TheTest.id).OrderByDescending(x => x.id).Take(Convert.ToInt32(model.TheTest.QuestionsNumber)).ToList();
            model.SingleQuee = db.Result.ToList();
            return View(model);
        }
    }
        [HttpPost]
        public ActionResult Test(int? id, List<Result> singleQuee)
        {
            using (KafoEntities db = new KafoEntities())
            {

                int result = 0;
                foreach (Result item in singleQuee)
                {
                    Result sets = db.Result.Where(x => x.id == item.id).FirstOrDefault();
                    sets.Question = item.Question;
                    db.SaveChanges();

                    var check = db.InTest.Where(x => x.Question == item.Question).FirstOrDefault();
                    if (check != null)
                    {
                        if (item.Question == "1")
                        {
                            result++;
                        }
                    }
                }

                return RedirectToAction("Results", "Exams", new { Controller = "Exams", Action = "Results", id = done.id });
            }

        }

我首先将来自单选按钮值的新字符串保存到结果记录中,然后在 if 条件中调用它来检查它的值

这里的问题是我得到了一个

对象引用未设置为对象的实例。

当我发布测试时,这意味着列表是空的,所以我需要知道是什么导致单选按钮不起作用, 谢谢。

【问题讨论】:

    标签: asp.net-mvc c#-5.0


    【解决方案1】:

    如果你想在 Mvc 中绑定一个对象列表,你应该将控制器命名为“ModelName[indx].PropertyName”。在您的情况下,它应该是“singleQuee[0].Question”。

    代码示例

    var Indx = 0;
    foreach (var item in Model.TheQuestions)
    {
        .....
        var radioName = $"singleQuee[{Indx}].Question";
        <div class="col-lg-7" style="text-align:right;margin-right:10px;">
            <div class="row">
                 <input type="radio" name="@radioName" value="1" />
                 <h5 style="padding-top:3px;padding-right:8px;">@item.RightAnswer</h5>
            </div>
       </div>
       .....
    }
    

    动作方法

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 2017-06-29
      • 2019-03-24
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2014-03-26
      相关资源
      最近更新 更多