【问题标题】:Check radio button based on database value根据数据库值检查单选按钮
【发布时间】:2019-09-16 17:27:46
【问题描述】:

我有一个 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 -->

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

【问题讨论】:

  • 您也应该将模型添加到您的问题中
  • 视图中模型的类是什么?
  • 视图是Due类
  • @HienNguyen 有什么帮助吗?

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


【解决方案1】:

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

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

【讨论】:

  • 它现在返回这个错误:'System.Collections.Generic.IEnumerable&lt;PassAllocation.Models.Due&gt;' does not contain a definition for 'status' and no extension method 'status' accepting a first argument of type 'System.Collections.Generic.IEnumerable&lt;PassAllocation.Models.Due&gt;' could be found (are you missing a using directive or an assembly reference?)
  • 哪个对象包含应用他们的状态的定义,比如if 's.Model'
猜你喜欢
  • 2016-05-08
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-25
  • 2013-09-07
  • 2016-12-10
  • 2013-10-29
相关资源
最近更新 更多