2

I'm having an issue with binding my model to view. When i try to create a radio button group with my Question model, view posting every boolean false.

Model

Edit: Changed fields to property.

public class Question
{
    public string Name { get; set; }
    public List<Answer> Answers { get; set; }

    public Question()
    {
        Answers = new List<Answer>();
    }
}

public class Answer
{
    public string AnswerText { get; set; }
    public bool Checked { get; set; }
    public long Id { get; set; }
    public long QuestionId { get; set; }
}

View

    for (int i = 0; i < Model.Answers.Count; i++)
    {
        @Html.HiddenFor(m => m.Answers[i].Id)
        @Html.HiddenFor(m => m.Answers[i].AnswerText)
        @Html.HiddenFor(m => m.Answers[i].QuestionId)


        @Html.DisplayFor(m => m.Answers[i].AnswerText)
        @Html.RadioButtonFor(m => Model.Name, Model.Answers[i].Checked)
        <br>
    }

Edit:

Page

Text

Debug

Text

Eren Peksen
  • 175
  • 2
  • 10

2 Answers2

1

The second parameter of RadioButtonFor is not a bool indicating whether or not to show select the radio button. It is just a value based on this:

Html.RadioButtonFor(model => property, object value, object htmlAttributes)

Model => property: it refers to a model property.

Object value: This value will be posted to server if radio button is selected.

So it is expected that it would not show as selected.

You need to do something like this:

@for (int i = 0; i < Model.Answers.Count; i++)
{
    @Html.RadioButtonFor(m => Model.Name, Model.Answers[i].Id)
}

That way you will bind the value to the id, and when you post you will get the correct id to handle.

Now if you set the Model.Name = SomeIdFromAnswers, then the corresponding radio button will be selected.

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • I tried this too but not working https://i.stack.imgur.com/ISRkD.png. Can't I use radio buttons like checkboxes, isn't there an attribute I can bind them to one group without changing name attribute? I tried to change them to ChecBoxFor and handle their value with jquery but it looks bad. – Eren Peksen Dec 04 '19 at 13:24
  • I was using Model.Name on another code block in my main project, when i cleaned my code I can get selected answer id with this bind. Thanks for the help and explanation for parameters :) – Eren Peksen Dec 05 '19 at 07:43
0
try this, it will work

      @{ 
      for (int i = 0; i < Model.Answers.Count; i++)
        {
            <div>
                @if(Model.Answers[i].Checked)
                {
                @Html.RadioButtonFor(model => Model.Name,Model.Answers[i].Id, new { id = "answer" +i})
                }

            </div>
        }
    }
  • It's not listing any radio button cause they coming false by default, i tried adding id attribute but didn't work. – Eren Peksen Dec 04 '19 at 11:49
  • you can check by removing @if(Model.Answers[i].Checked), it will work, i have checked it. it is working here in my project. – Ashutosh Kushawaha Dec 04 '19 at 11:59
  • still not sets 'Checked' property to true when model comes to controller. It's only working with CheckBoxFor, but I'm trying to create a survey with multiple and single choices depend on a parameter. – Eren Peksen Dec 04 '19 at 13:15