1

I want to create multiple grouped Radio buttons. So, basically I have multiple questions and 3 radio buttons with answers. The problem I'm facing is how to return the selected value for the multiple radio buttons.

Controller

public IActionResult Test()
    {
        SafetyObservationCardForm form = new SafetyObservationCardForm();
        using (IDbConnection dbConnection = Connection)
        {
            dbConnection.Open();
            var questions = dbConnection.Query<QuestionsViewModel>("SELECT * FROM SOCQuestions ORDER BY soc_order_id");

            var observationType = new List<ObservationType>
            {
                new ObservationType { Id = 1, Name = "Safe" },
                new ObservationType { Id = 2, Name = "At Risk" },
                new ObservationType { Id = 3, Name = "N/A" }
            };

            var rb = new List<Answer>();
            foreach (var item in questions)
            {
                rb.Add(new Answer { Id = item.soc_question_id, ObservationTypes = observationType }); 
            };

            form.Answers = rb;
            form.Questions = questions.ToList();

            return View(form);
        }
    }

Model

public class SafetyObservationCardForm
{
    public List<QuestionsViewModel> Questions { get; set; }
    public string Comments { get; set; }
    public string Location { get; set; }
    public string Observer { get; set; }
    public DateTime TimeStamp { get; set; }
    public string Task { get; set; }
    public ObserverType ObserverType { get; set; }
    public IEnumerable<Answer> Answers { get; set; }
}
public class ObserverType
{
    public bool Supervisor { get; set; }
    public bool Peer { get; set; }
    public bool Self { get; set; }
    public bool Other { get; set; }
}

public class ObservationType
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Answer
{
    public int Id { get; set; }
    public IEnumerable<ObservationType> ObservationTypes { get; set; }
    public int SelectedObservation { get; set; }
    }
}

View

Here I'm trying the loop. Answers model is null when I submit the form for some reason.

@model DBI.Safety.Models.SafetyObservationCardForm
<form asp-action="CreateTest">
@foreach (var answer in Model.Answers)
{
    <input name="@answer.Id" asp-for="@answer.SelectedObservation" type="radio" 
    value="@answer.Id" /> @answer.ObservationTypes
}
</form>
dsub camfx
  • 21
  • 1
  • 5
  • In the view, are you writing a
    at all? If so, please include?
    – Jacob M. May 21 '18 at 21:56
  • What is the model in your view? What are you wanting to bind the selected option to? (your `name="@answer.Id"` makes no sense) –  May 21 '18 at 23:00
  • @StephenMuecke I put name="@answer.Id" to give a unique name for the radio button. Otherwise all the radio buttons will be in one category. I'm not sure if this is the right approach. – dsub camfx May 22 '18 at 12:32
  • That makes no sense. For a start, your radio buttons need to be grouped (with each group having the same `name` attribute, and the `name` attribute needs to be the name of the property you are binding to. –  May 22 '18 at 12:38
  • Suggest you start by reading [this answer](https://stackoverflow.com/questions/28055287/asp-net-mvc-5-group-of-radio-buttons/28057533#28057533) and [this answer](https://stackoverflow.com/questions/31128152/mvc-adding-values-of-radio-buttons-checked-into-a-collection-and-saving-to-data/31128309#31128309) –  May 22 '18 at 12:39
  • @StephenMuecke Thank you for the answer. That did work. – dsub camfx May 22 '18 at 15:04

0 Answers0