【问题标题】:How to bind multiple radio button to MVC controller如何将多个单选按钮绑定到 MVC 控制器
【发布时间】:2020-01-19 13:47:19
【问题描述】:

我有一个带有多个单选按钮的视图,这些按钮没有绑定到控制器操作方法。

查看

@model IEnumerable<Quizz.Models.QuestionTable>
@{
    ViewBag.Title = "Index";
}
<html>
<head>
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.4.1.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('.classname').change(function () {
                $.post("",
                    {
                        qid: $(this).attr("name"),
                        answer: $(this).val()
                    },
                    function (data, status) {
                    });
            });

            $("#viewScore").click(function () {
                $.post("/Exam/ViewScore",
                    function (data, status) {
                       // alert("Data: " + data + "\nStatus: " + status);
                        $("#result").html(data);
                    });
            });
        });

    </script>
</head>

<h2>Index</h2>
<div>

    @foreach (var item in Model)
    {
        <br />@Html.DisplayFor(x => item.question)<br />
        @Html.RadioButton(item.qid.ToString(), item.op1, false, new { @class = "classname" })@item.op1 <br />
        @Html.RadioButton(item.qid.ToString(), item.op2, false, new { @class = "classname" })@item.op2 <br />
        @Html.RadioButton(item.qid.ToString(), item.op3, false, new { @class = "classname" })@item.op3 <br />
        @Html.RadioButton(item.qid.ToString(), item.op4, false, new { @class = "classname" })@item.op4 <br />
    }

</div>
<div>
    <input type="submit" value="Check Score" id="viewScore"/>
</div>

<h2 id="result"></h2>
</html>

控制器

using Quizz.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Diagnostics;
using System.Data.SqlClient;

namespace Quizz.Controllers
{
    public class ExamController : Controller
    {
        public static List<QAns> answerList = new List<QAns>();
        // GET: Exam
        public ActionResult Index()
        {
            QuizzDBEntities db = new QuizzDBEntities();
            var model = db.QuestionTables.ToList();
            return View(model);
        }


        [HttpPost]
        public ActionResult Index(QAns ans)
        {
            answerList.Add(ans);
            return new EmptyResult();
        }

        [HttpPost]
        public ActionResult ViewScore()
        {
            //Check score
            int score = 0;
            foreach (QAns ans in answerList)
            {
                SqlConnection conn = new SqlConnection("data source=DESKTOP-137HJKH\\SQLEXPRESS;initial catalog=QuizzDB;integrated security=True");
                conn.Open();
                SqlCommand command = new SqlCommand("Select qid from QuestionTable where qid="+ ans.qid + " and answer='"+ ans.answer + "'", conn);           
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {

                        score++;
                    }
                }
            }       
            answerList.Clear();
            return Content("Your scorre is " + score);//new EmptyResult();
        }
    }
}

我没有从视图中获取所选单选按钮的列表到控制器操作方法。请帮忙。

我将模型对象列表传递给视图。在单选按钮中使用,并且在更改按钮时 post 方法没有获取值。

【问题讨论】:

  • 当您说“没有从视图中获取选定单选按钮的列表到控制器操作方法”时,您是否收到任何异常消息?您是否正在采取行动但 ans 参数为 null 或不为 null 但未设置值?

标签: c# asp.net-mvc


【解决方案1】:

尝试将您的控件包装在@using(Html.BeginForm()) 中 并且您的控制器方法应该采用FormCollection 类型的参数 查看以下answer了解更多信息

【讨论】:

    猜你喜欢
    • 2016-01-31
    • 2020-09-02
    • 2014-11-24
    • 1970-01-01
    • 2023-03-18
    • 2019-03-24
    • 2011-03-29
    • 1970-01-01
    • 2017-02-12
    相关资源
    最近更新 更多