根据数据库值检查单选按钮

时间:2019-04-27 12:19:27

标签: asp.net-mvc razor radio-button

我有一个Dues表,我想根据其数据库值(是否已清欠)选择单选按钮。

我有3个不同的单选按钮,它们分别作为int值插入数据库中。

任务:

public partial class Due
{
    public int Id { get; set; }
    public Nullable<int> DuesTypeId { get; set; }
    [Required(ErrorMessage = "SF No is required")]
    public Nullable<int> SF_No { get; set; }
    [Required(ErrorMessage = "Amount is required")]
    [DataType(DataType.Currency, ErrorMessage = "Only numbers can be enter")]
    [DisplayFormat(DataFormatString = "{0:N2}")]
    public Nullable<int> Amount { get; set; }
    public Nullable<int> Status { get; set; }    

    public virtual DuesStatus DuesStatus { get; set; }
    public virtual DuesType DuesType { get; set; }
    public virtual Family Family { get; set; }
}    

DuesStatus:

public partial class DuesStatus
{
    public DuesStatus()
    {
        this.Dues = new HashSet<Due>();
    }

    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Due> Dues { get; set; }
}

查看:

@model IEnumerable<PassAllocation.Models.Due>

@{
ViewBag.Title = "Index";
var status = ViewBag.Status as List<PassAllocation.Models.DuesStatus>;
}

<h3>Dues</h3>
<div class="row">
<div class="col-lg-12">
    <div class="panel panel-default">
        <div class="panel-heading">
            Families Dues
        </div>
        <!-- /.panel-heading -->
        <div class="panel-body">
            <table width="100%" class="table table-striped table-bordered table-hover" id="familydues">
                <thead>
                    <tr>
                        <th>SF No</th>
                        <th>Head of Family</th>
                        <th>Amount</th>
                        <th>Status</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr>
                        <th>SF No</th>
                        <th>Head of Family</th>
                        <th>Amount</th>
                        <th>Status</th>
                    </tr>
                </tfoot>
                <tbody>
                    @foreach (var item in Model)
                    {
                        <tr id="@item.Id">
                            <td>
                                @Html.DisplayFor(modelItem => item.Family.SF_No)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Family.Members.Where(x => x.Member_Type == 1).FirstOrDefault().Full_Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Amount)
                            </td>
                            <td>
                                    @foreach (var s in status)
                                    {
                                        <label class="Cont">
                                        @Html.RadioButtonFor(m=>status,s.Id, new { @class = "Status ", @checked = "checked"}) @s.Name
                                        <span class="checkmark"></span>
                                        </label>
                                    }
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
        <!-- /.panel-body -->
    </div>
    <!-- /.panel -->
</div>
<!-- /.col-lg-12 -->

我想要这样的结果,但是我无法选择要保存数据的单选按钮。

enter image description here

1 个答案:

答案 0 :(得分:0)

使用此三元运算符来处理条件。

@Html.RadioButtonFor(m=>status,s.Id, Model.status== true ? new { Checked = "checked" ,@class = "Status "} : new { @class = "Status "})