bind< string,string>词典到复选框MVC(制作复选框返回srtring值)

发布于 2025-02-06 16:43:53 字数 3117 浏览 5 评论 0原文

我在ViewModel

 public Dictionary<string, string> Answers { get; set; }
 public List<KeyValuePair<Question, List<string>>> QuestionsAndOptions { get; set; }

    public TestVM()
    {
        Answers = new Dictionary<string, string>();
        QuestionsAndOptions = new List<KeyValuePair<Question, List<string>>>();

    }

QuestionandOptions中包含问题及其对我的测试页面的选项IM传递到查看的选项。 我的项目中有不同的问题类型,其中有多个答案选项是其中之一。

答案包含&lt; string,string&gt;问题ID和用户答案对

我的视图中我得到了此代码:

  @for (int i = 0; i < Model.QuestionsAndOptions.Count; i++)
            {
                var question = Model.QuestionsAndOptions[i];
                <tr>
                    <td>
                        <b>@question.Key.Text</b>
                        @if (question.Key.TypeId == 1)  /*single, radiobuttons*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.RadioButton("testVM.Answers[" + question.Key.Id + "].Value", answer, false) @answer
                            }

                        }
                        @if (question.Key.TypeId == 2)  /*multiple, checkboxes*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.CheckBox("testVM.Answers[" + question.Key.Id + "].Value", answer) @answer

                            }

                        }
                        @if (question.Key.TypeId == 3)   /*string text, textbox*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.TextBox("testVM.Answers[" + question.Key.Id + "].Value")
                            }

                        }

第一和第三,如果(){...}正常工作,我在提交后得到了我需要的东西,但是对于复选框,我显然是正确的/false

我希望复选框通过字符串(检查值)来回答字典

I got this structure in my ViewModel

 public Dictionary<string, string> Answers { get; set; }
 public List<KeyValuePair<Question, List<string>>> QuestionsAndOptions { get; set; }

    public TestVM()
    {
        Answers = new Dictionary<string, string>();
        QuestionsAndOptions = new List<KeyValuePair<Question, List<string>>>();

    }

QuestionsAndOptions contains Questions and its options for my Test Page im passing to VIew.
I got different question types in my project and questions with multiple answer options is one of them.

Answers contains <string,string> pairs of question IDs and user Answer for it

I got this code in my View:

  @for (int i = 0; i < Model.QuestionsAndOptions.Count; i++)
            {
                var question = Model.QuestionsAndOptions[i];
                <tr>
                    <td>
                        <b>@question.Key.Text</b>
                        @if (question.Key.TypeId == 1)  /*single, radiobuttons*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.RadioButton("testVM.Answers[" + question.Key.Id + "].Value", answer, false) @answer
                            }

                        }
                        @if (question.Key.TypeId == 2)  /*multiple, checkboxes*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.CheckBox("testVM.Answers[" + question.Key.Id + "].Value", answer) @answer

                            }

                        }
                        @if (question.Key.TypeId == 3)   /*string text, textbox*/
                        {
                            foreach (var answer in question.Value)
                            {
                                <input type="hidden" name="testVM.Answers[@question.Key.Id].Key" value="@question.Key.Id" />
                                <input type="hidden" name="testVM.Answers.Index" value="@question.Key.Id" />
                                <input type="hidden" name="testId" value="@Model.Test.Id" />
                                @Html.TextBox("testVM.Answers[" + question.Key.Id + "].Value")
                            }

                        }

First and third If(){...} works fine and i getting what i need after submit, but for checkboxes i'm obviously geting true/false

I want checkboxes to pass strings (checked values) to Answer dictionary

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

焚却相思 2025-02-13 16:43:53

如果要将值绑定到复选框,则可以尝试在复选框中设置value,尝试使用以下代码:

<input type="checkbox" value=@answer name="testVM.Answers[" + question.Key.Id + "].Value" />@answer

选中复选框时复选框的值(@answer )将传递给行动。

If you want to bind value to checkbox,you can try to set value in checkbox,try to use the following code:

<input type="checkbox" value=@answer name="testVM.Answers[" + question.Key.Id + "].Value" />@answer

When the checkbox is checked the value of the checkbox(@answer) will be passed to action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文