0

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

Yiyi You
  • 16,875
  • 1
  • 10
  • 22

1 Answers1

0

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.

Yiyi You
  • 16,875
  • 1
  • 10
  • 22