0

I am trying to create a group of radio buttons on my form. I am using Razors Html.RadioButtonFor, but I am uncertain how to properly populate my Radios with the appropriate values from my model.

for (int i = 0; i < Model.Questions.Count(); i++)
{
    @Html.RadioButtonFor(m => m.Questions[i].Selected, new SelectList(Model.Questions[i].Options, "Id", "Text"))
}

As you can probably notice I was trying to do the same thing you can do in a dropdownfor since they are basically the same thing.

MODELS:

public class QuestionViewModel
    {
        public int? Id { get; set; }

        public string QuestionType { get; set; }

        public string SubType { get; set; }

        public string Text { get; set; }

        public int SortOrder { get; set; }

        public bool IsHidden { get; set; }

        public int Selected { get; set; }

        public List<QuestionOptionViewModel> Options { get; set; }

    }
public class QuestionOptionViewModel
    {
        public int? Id { get; set; }

        public string Text { get; set; }

        public string Value { get; set; }

        public bool IsChecked { get; set; }

        public int Selected { get; set; }
    }
shenku
  • 11,969
  • 12
  • 64
  • 118
allencoded
  • 7,015
  • 17
  • 72
  • 126
  • I think What you want is this http://stackoverflow.com/questions/10805502/mvc-razor-radio-button – user1477388 Feb 20 '14 at 19:23
  • Not exactly. I am trying to create a radiobuttonfor like you can do with dropdownfor. Like this post buy I want to do it with radiobuttons >> http://stackoverflow.com/questions/21912140/trouble-creating-dropdownlistfor-with-razor/21912718?noredirect=1#comment33195666_21912718 – allencoded Feb 20 '14 at 19:33
  • You shouldn't ask the same question more than once. – user1477388 Feb 20 '14 at 19:35
  • Its not the same question. One is dropdown and the other is a radio. They both have very unique answers. – allencoded Feb 20 '14 at 19:37
  • Ah, indeed. Sorry for the confusion. – user1477388 Feb 20 '14 at 19:46

1 Answers1

3

Check the below solution. I slightly changed your models -

public class QuestionMainModel
{
    public List<QuestionViewModel> Questions { get; set; }
}

public class QuestionViewModel
{
    public int? Id { get; set; }
    public string Text { get; set; }
    public List<QuestionOptionViewModel> Options { get; set; }
    public int Selected { get; set; }
}
public class QuestionOptionViewModel
{
    public int? Id { get; set; }
    public string Text { get; set; }
    public string Value { get; set; }        
}

The controller action which populates the view -

    public ActionResult Index()
    {
        QuestionMainModel model = new QuestionMainModel();

        List<QuestionOptionViewModel> options = new List<QuestionOptionViewModel>();
        options.Add(new QuestionOptionViewModel(){ Id = 1, Text = "Ans1", Value = "1"});
        options.Add(new QuestionOptionViewModel(){ Id = 2, Text = "Ans2", Value = "2"});
        options.Add(new QuestionOptionViewModel(){ Id = 3, Text = "Ans3", Value = "3"});

        model.Questions = new List<QuestionViewModel>();
        model.Questions.Add(new QuestionViewModel() { Id = 1, Text = "Question1", Options = options });

        return View(model);
    }

The view which is being displayed is as follows -

@model MVC.Controllers.QuestionMainModel

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@using (Html.BeginForm("Submit", "sample", FormMethod.Post))
{

    for (int i = 0; i < Model.Questions.Count; i++)
    {
        @Html.HiddenFor(m => m.Questions[i].Id)
        @Html.LabelFor(m => m.Questions[i].Text, Model.Questions[i].Text)
        for (int j = 0; j < Model.Questions[i].Options.Count; j++)
        {
            @Html.RadioButtonFor(m => m.Questions[i].Selected, Model.Questions[i].Options[j].Value) @Model.Questions[i].Options[j].Text
        }
    }

    <input type="submit" value="Click" />
}

And when you click submit button, it will hit below controller action -

    public ActionResult Submit(QuestionMainModel model)
    {
        return null;
    }

And the selected answer would be as follows -

enter image description here

NOTE: If you want to post all the options and Question Text, then use HiddenFields, just like I did for Is.

ramiramilu
  • 17,044
  • 6
  • 49
  • 66