【问题标题】:C# MVC Radio Button Names and Values groups pass into [HTTPPost]C# MVC 单选按钮名称和值组传递到 [HTTPPost]
【发布时间】:2021-05-21 17:20:11
【问题描述】:

大家好,我是新手,所以对我来说太赤裸裸了。我在 C# MVC5 中有一个非常简单的调查构建,我需要从 5 个问题的分组单选按钮中收集结果。

每个问题大约有 3-4 个选项。我设法将问题和选择呈现到我的 cshtml 中。现在我正在思考如何捕获数据并将其保存到字典或列表中以将其传递给控制器​​。这是我需要一点帮助的地方。在模型中,如果需要,我有一个 ListAnswers 类,它可以保存来自收音机的值,最重要的要捕获的是 QuestionID 和 Choice Option,无论是文本还是选择的 id。

请在我学习时使用非常初级的编码。谢谢。

@model PMLessonLearned.Models.ViewSurvey
@using (Html.BeginForm("Surveys", "Home"))
{
    @Html.AntiForgeryToken()
    foreach (var qu in Model.Questions)
    {
        <dt>
            @Html.DisplayFor(modelItem => qu.Question)
        </dt>
        foreach (var ch in Model.Choices)
        {
            if (qu.QID == ch.QuestionID)
            {
                <dd>
                    <div>
                        @Html.RadioButton("Q_" + ch.QuestionID, ch.ChoiceOption)
                        @Html.DisplayFor(modelItem => ch.ChoiceOption)
                    </div>
                </dd>
            }
        }
    }
    <dt>
        <input type="submit" class="btn btn-primary btn-sm" value="Submit" />
    </dt>
}

这是按名称 = Q_1、Q_2 等(使用 ch.QuestionID)下的组捕获的,ch.ChoiceOption 是实际措辞:“是”、“否”、“不确定”等。

HTML 示例:

<input id="Q_4" name="Q_4" type="radio" value="All the time">
<input id="Q_2" name="Q_2" type="radio" value="No">

这是我的 HomeController:

         [HttpGet]
        public ActionResult Surveys()
        {
            ViewSurvey m = new ViewSurvey();
            m.PopulateQuestions();
            m.PopulateChoices();
            return View(m);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Surveys(ViewSurvey m)
        {
            var testrestuls = m;
            return RedirectToAction("Index", "Home");
        }

ShowSurvey.cs 模型:

        public class ListQuestions
    {
        public int QID { get; set; }
        public string Question { get; set; }
    }
    public class ListChoices
    {
        public int CID { get; set; }
        public int QuestionID { get; set; }
        public string ChoiceOption { get; set; }
    }
    public class ListAnswers
    {
        public int AID { get; set; }
        public int AQuestionID { get; set; }
        public string AnswerText { get; set; }
        public int ChoiceID { get; set; }
    }
    public class ViewSurvey
    {
        public IEnumerable<ListQuestions> PopulateQuestions()
        {
            Entitydb db = new Entitydb();
            var m = (from e in db.SurveyQuestions
                     select new ListQuestions()
                     {
                         QID = e.ID,
                         Question = e.Question
                     }).ToList();
            return m;
        }
        public IEnumerable<ListChoices> PopulateChoices()
        {
            Entitydb db = new Entitydb();
            var m = (from e in db.SurveyQuestions
                     join c in db.SurveyQuestionChoices on e.ID equals c.QuestionID
                     select new ListChoices()
                     {
                         CID = c.ID,
                         QuestionID = c.QuestionID,
                         ChoiceOption = c.Choice,
                     }).ToList();
            return m;
        }
    }

这是它的显示方式: Survey Look

【问题讨论】:

    标签: asp.net-mvc dictionary asp.net-core razor radio-button


    【解决方案1】:

    带有name属性的Asp.net核心绑定模型。您可以更改radiobutton的名称值。并添加QuestionId和choiceOption的隐藏输入。

    这是一个演示,在每个单选按钮之后,我添加了一个带有选择选项的隐藏输入,在表单提交之前,我在选中单选按钮后将隐藏输入的名称更改为正确的格式。

    查看:

    @using (Html.BeginForm("Surveys", "Home"))
    {
        @Html.AntiForgeryToken()
        var i = 0;
        foreach (var qu in Model.Questions)
        {
            <input hidden name="Answers[@i].AQuestionID" value=@qu.QID>
            <dt>
                @Html.DisplayFor(modelItem => qu.Question)
            </dt>
            foreach (var ch in Model.Choices)
            {
                if (qu.QID == ch.QuestionID)
                {
                    <dd>
                        <div>
                            @Html.RadioButton("Answers[" + i + "].ChoiceID", ch.CID)
                            @Html.TextBoxFor(modelItem => ch.ChoiceOption, new { @hidden = "hidden" })
                            @*@Html.EditorFor(modelItem => ch.ChoiceOption, new { @hidden = true })*@
                            @*<input asp-for=@ch.ChoiceOption hidden />*@
                            @Html.DisplayFor(modelItem => ch.ChoiceOption)
                        </div>
                    </dd>
                }
            }
            i++;
        }
        <dt>
            <input type="submit" class="btn btn-primary btn-sm" value="Submit" />
        </dt>
    }
    <script>
            $("form").submit(function () {
                $("input[type='radio']").each(function () {
                    if ($(this).is(":checked")) {
                        $(this).next().attr("name", $(this).attr("name").split("ChoiceID")[0] + "AnswerText");
                    }
                })
            })
        </script>
    

    型号:

    public class ListQuestions
        {
            public int QID { get; set; }
            public string Question { get; set; }
        }
        public class ListChoices
        {
            public int CID { get; set; }
            public int QuestionID { get; set; }
            public string ChoiceOption { get; set; }
        }
        public class ListAnswers
        {
            public int AID { get; set; }
            public int AQuestionID { get; set; }
            public string AnswerText { get; set; }
            public int ChoiceID { get; set; }
        }
        public class ViewSurvey
        {
            public IEnumerable<ListQuestions> Questions { get; set; }
            public IEnumerable<ListChoices> Choices { get; set; }
        }
    

    控制器(我用假数据来测试):

    [HttpGet]
            public ActionResult Surveys()
            {
                ViewSurvey v = new ViewSurvey
                {
                    Questions = new List<ListQuestions> {
                    new ListQuestions{ QID=1, Question="How often during the lifecycle of your project you use lesson learned?" },
                    new ListQuestions{ QID=2, Question="Do you know how to build a lesson learned template?" },
    
                 },
                    Choices = new List<ListChoices> {
                        new ListChoices{  CID=11,QuestionID=1, ChoiceOption="All the Time"},
                        new ListChoices{  CID=12,QuestionID=1, ChoiceOption="Sometimes"},
                        new ListChoices{  CID=13,QuestionID=1, ChoiceOption="Never"},
                        new ListChoices{  CID=14,QuestionID=1, ChoiceOption="Rarely"},
                        new ListChoices{  CID=21,QuestionID=2, ChoiceOption="No"},
                        new ListChoices{  CID=22,QuestionID=2, ChoiceOption="Yes"},
                        new ListChoices{  CID=23,QuestionID=2, ChoiceOption="Sometimes"},
    
    
    
                    }
                };
                return View(v);
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult Surveys(List<ListAnswers> Answers)
            {
                return Ok();
            }
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多