0

I have a model called AnswerSheet, it is a bunch of String Answers. Snip below:

public class PassengerAnswerSheet
    {
        public int ID { get; set; }

        [Required]
        [Display(Name = "Question 1")]
        public string Q1 { get; set; }

        [Required]
        [Display(Name = "Question 2")]
        public string Q2 { get; set; }

For each answer, I have multiple checkboxes, I want the answer to be a string generated by the selected checkboxes text.

The checkboxes are built using a different model, an animal for this example:

public class Animal
    {
        public bool IsChecked { get; set; }
        public String Name { get; set; }
    }

Currently I can get the Question to display what I want (sort of) using this code on my Razor Form:

<div class="form-group">
            @Html.LabelFor(model => model.Q1, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @foreach(var  animal in Curbside.Helpers.AnswerLists.CreateAnimalsList())
                {
                    string Hi = (animal.Name);  
                    @Html.CheckBoxFor(item => animal.IsChecked)
                    @Hi 
                    <br />
                }
                @Html.EditorFor(model => model.Q1)                
                @Html.ValidationMessageFor(model => model.Q1)
            </div>
        </div>

Now what I need to figure out, is how to have the checked boxes for the "Foreach" section generate a string when the user submits a form, example it will generate "Cow, Pig" as the answer for Q1 if the user has checked the boxes beside Cow and Pig. I've been reading a lot on this but haven't figured it out, and I feel stuck here. Will continue to investigate. Obviously I need ot get rid of @Html.EditorFor but I don't know what to replace it with or how to use it.

dave317
  • 754
  • 2
  • 12
  • 30
  • The first thing you should be doing is creating a view model to represent what you want to display (something along the lines of [this answer](http://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533)) –  May 11 '16 at 22:52
  • Even if i create a viewmodel I will still be facing the same problem. I'll create a viewmodel and try this just to be sure. I've found multiple different ways to display what i want, the problem is on SUBMIT how do I get it to save the selected boxes. – dave317 May 11 '16 at 23:23
  • If you use a view model you will not be facing any problems :) –  May 11 '16 at 23:24
  • And as a side note, you cannot use a `foreach` loop to generate form controls for a collection (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943)) –  May 11 '16 at 23:27
  • Check this link if it might be of any help http://stackoverflow.com/questions/36981588/get-checked-check-box-value-using-form-collection/36984315#36984315 and also check this http://stackoverflow.com/questions/8624762/performing-httppost-to-store-checkbox-selected-values-in-database – Rica May 12 '16 at 09:44

0 Answers0