Проверьте переключатель на основе значения базы данных - PullRequest
0 голосов
/ 27 апреля 2019

У меня есть таблица Dues, и я хотел выбрать радиокнопку на основе значения ее базы данных, либо очищенные взносы, либо нет.

У меня есть 3 разные радиокнопки, которые вставляются как значения intв моей базе данных.

Dues:

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 голосов
/ 29 апреля 2019

Используйте этот троичный оператор для обработки условия.

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