【问题标题】:Dropdownlist with 2 columns in c#?c#中有2列的下拉列表?
【发布时间】:2019-10-01 10:29:53
【问题描述】:

我有一个使用 MVC 和 .Net 和 EF6 构建的网站。我想在一个视图中显示一个带有类名和姓氏的下拉列表。

我该怎么做?

我的代码是:

 @Html.DropDownList("ShiftManagerId", null, htmlAttributes: new { @class = "form-control" })

班级是:

public class ShiftCalendar
  {
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public int ShiftId { get; set; }
    public virtual Shift Shift { get; set; }

    public DateTime Date { get; set; }
    public DateTime ShiftEndTime { get; set; }        

    public User ShiftManager { get; set; }
    public string ShiftManagerId { get; set; }
  }
}

【问题讨论】:

    标签: c# entity-framework model-view-controller


    【解决方案1】:

    试试这个

    在你的 ShiftCalendar 类中添加:

    public IEnumerable<SelectListItem> ShiftManagersList
    {
        get {  var managers = dbContext.ShiftManagers.Select(f => new SelectListItem
                                               {
                                                   Value = f.Id.ToString(),
                                                   Text = $"{f.Name} {f.Surname}"           
                                              });
              return managers;
            }
    }
    

    然后在你看来尝试这样的事情:

    @Html.DropDownListFor(m => m.ShiftManagerId, Model.ShiftManagersList, new { @class = "form-control" })
    

    请记住,我不知道您的 dbContext 和用户属性名称的确切名称。所以相应地更改代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 2018-02-18
      • 2015-10-29
      • 1970-01-01
      • 2019-04-07
      • 2020-06-18
      相关资源
      最近更新 更多