简体   繁体   English

试图让下拉列表在asp.net Core中工作

[英]Trying to get a dropdown working in asp.net core

I am trying to get a dropdown working in asp.net core but it doesnt even appear to like the first list that its supposed to populate staff memebers with. 我试图在asp.net core中使用下拉菜单,但它似乎并不喜欢它应该用来填充工作人员的第一个列表。 I have a table called activity header with a foreignkey field staff id linked to a staff table. 我有一个称为活动标题的表,该表的外键字段人员ID链接到人员表。

First here is the code I am trying from this tutorial 首先,这是我在本教程中尝试的代码

public IEnumerable<SelectListItem> GetStaff()
{
        List <SelectListItem> staff = _db.Staff.AsNoTracking();

            List <SelectListItem> selectListItems = _db.Staff.AsNoTracking()
                .OrderBy(n => n.FirstName)
                    .Select(n =>
                    new SelectListItem
                    {
                        Value = n.StaffID.ToString(),
                        Text = n.FirstName.ToString() + " " + n.LastName.ToString()
                    }).ToList();
            var stafftip = new SelectListItem()
            {
                Value = null,
                Text = "--- select staff memeber---"
            };
            staff.Insert(0, stafftip );
            return new SelectList(staff, "Value", "Text");

}

My Class for activity header 我的活动标题类

 public class ActivityHeader
 { 
    // other fields removed for brevity

    [Required]        
    [Display(Name = "Staff")]
    public int StaffId { get; set; }
    public IEnumerable<Staff> Staff { get; set; }
 }

My staff class here which should link to the other table with the staff id 我的工作人员班级应该链接到带有工作人员ID的其他表

public class Staff
{
    [Key]
    public int StaffID { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }
    public int DepartmentId { get; set; }

    public string StaffNumber { get; set; }
    public virtual IEnumerable<Staff> Staff { get; set; }
 }

This is my html for my view which is inside a bootstrap popup. 这是我的html视图,位于引导弹出窗口中。

<div class="col-sm-3">
     <label for="inputPostalCode">SOP</label>
   <div class="form-group">
     @Html.LabelFor(x => Model.Staff, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-5">
       @Html.DropDownListFor(x => Model.StaffId, new SelectList(Model.Staff, "Value", "Text"), htmlAttributes: new { @class = "form-control", id = "Country" })
     @Html.ValidationMessageFor(x => x.StaffId, "", new { @class = "text-danger" })
  </div>
 </div>
</div>

What i expect to see is a html drop down with the results from staff but linked to activity head with staff id. 我希望看到的是带有工作人员结果的html下拉列表,但链接到带有工作人员ID的活动头。

But it doesn't appear to like this list for some reason. 但是由于某种原因,它似乎并不喜欢此列表。 Exact error is 确切的错误是

在此处输入图片说明

I see there are few mistakes in your DropDownList implementation: 我看到您的DropDownList实现中有一些错误:

Number #1 : 1号 :

If you want if no item is selected from DropDownList then null value for the select list will be passed then don't need to set DropDownList level text as follows: 如果要从DropDownList未选择任何项目,则将传递选择列表的null值,则无需设置DropDownList级文本,如下所示:

var stafftip = new SelectListItem()
{
            Value = null,
            Text = "--- select staff memeber---"
};
staff.Insert(0, stafftip );

Rather simply your GetStaff() method as follows: 而是简单地按如下方式获取您的GetStaff()方法:

public SelectList GetStaff()
{
     var staffList = _db.Staff.Select(s => new
     {
           Value = s.StaffID,
           Text = s.FirstName + " " + s.LastName
     }).ToList();

    return new SelectList(staffList, "Value", "Text");
}

Now in your model classes, replace public virtual IEnumerable<Staff> Staff { get; set; } 现在,在模型类中,替换public virtual IEnumerable<Staff> Staff { get; set; } public virtual IEnumerable<Staff> Staff { get; set; } public virtual IEnumerable<Staff> Staff { get; set; } with public SelectList Staff { get; set; } public virtual IEnumerable<Staff> Staff { get; set; }public SelectList Staff { get; set; } public SelectList Staff { get; set; }

Then in the view: 然后在视图中:

@Html.DropDownListFor(x => Model.StaffId, Model.Staff,"Select Staff", htmlAttributes: new { @class = "form-control", id = "Country" })

Number #2: 2号:

If you want, if no item is selected from DropDownList then default (0 or anything) value for the select list will be passed then write your GetStaff() method as follows: 如果需要,如果没有从DropDownList选择任何项目,则将传递选择列表的default (0 or anything)值),然后按如下所示编写GetStaff()方法:

public IEnumerable<SelectListItem> GetStaff()
{
    List<SelectListItem> staffSelectListItems = _db.Staff.OrderBy(n => n.FirstName)
                                   .Select(n => new SelectListItem
                                   {
                                       Value = n.StaffID.ToString(),
                                          Text = n.FirstName.ToString() + " " + n.LastName.ToString()
                                   }).ToList();

    var defaultSelection = new SelectListItem()
    {
                Value = "0",
                Text = "Select Staff Member",
                Selected = true // <-- this is obligatory
    };
    staffSelectListItems.Insert(0, defaultSelection);
    return staffSelectListItems;
}

Then in the view: 然后在视图中:

@Html.DropDownListFor(x => Model.StaffId,(List<SelectListItem>)Model.Staff, htmlAttributes: new { @class = "form-control", id = "Country" })

Hope it will now work for you! 希望它现在对您有用!

public IEnumerable<SelectListItem> GetStaff()
{
    List <SelectListItem> selectListItems = _db.Staff.AsNoTracking()
         .OrderBy(n => n.FirstName)
         .Select(n =>
            new SelectListItem
                 {
                     Value = n.StaffID.ToString(),
                     Text = n.FirstName.ToString() + " " + n.LastName.ToString()
                 }).ToList();
    var stafftip = new SelectListItem()
       {
          Value = null,
          Text = "--- select staff memeber---"
       };

   selectListItems.Insert(0, stafftip );
   return new SelectList(selectListItems, "Value", "Text");        
}

You were trying to assign an IQueryable to a List which wasn't going to work. 您试图将IQueryable分配给无法使用的列表。 I have removed that line and fixed your use of the selectlist as staff is not needed. 我已删除了该行,并修复了您对选择列表的使用,因为不需要工作人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM