RadioButton doesn't get current value from model

橙三吉。 提交于 2020-01-17 08:37:31

问题


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.



来源:https://stackoverflow.com/questions/16587110/radiobutton-doesnt-get-current-value-from-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!