RadioButton doesn't get current value from mod

2019-08-28 00:18发布

问题:

My model is IEnumerable<> of RatingSource.

public class RatingSource
    {
        public int Id;

        public string Source;

        public bool IsActive;

In the view I want to see 2 columns: Source and radioButton, cheked if IsActive is true. I write:

@foreach (var ratingSource in Model)
 {
     <tr>
        <td>
            @Html.DisplayFor(modelItem => ratingSource.Source) @Html.RadioButton("Active",ratingSource.IsActive) <br />
        </td>
     </tr>
 }

But radioButtons are always unchecked. Where is a mistake?

回答1:

have you tried:

@Html.RadioButtonFor(m => ratingSource.IsActive)

You could also try

@Html.RadioButton("IsActive",ratingSource.IsActive)

EDIT:

@Html.RadioButton("Active", ratingSource.Id, ratingSource.IsActive)

This works, however, since radiobuttons are mutually exclusive, the last 1 that is active will be the one checked.