RadioButton无法从模型获取当前值 [英] RadioButton doesn't get current value from model

查看:101
本文介绍了RadioButton无法从模型获取当前值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型是RatingSource的IEnumerable<>.

My model is IEnumerable<> of RatingSource.

public class RatingSource
    {
        public int Id;

        public string Source;

        public bool IsActive;

在视图中,我想查看2列:Source和radioButton,如果IsActive为true,则选中该列.我写:

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>
 }

但是radioButtons始终未选中.哪里有错?

But radioButtons are always unchecked. Where is a mistake?

推荐答案

您是否尝试过:

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

您也可以尝试

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

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

但是,这是可行的,因为单选按钮是互斥的,所以最后一个处于活动状态的1将被选中.

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

这篇关于RadioButton无法从模型获取当前值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆