【问题标题】:Why doesn't my SelectList like contents of my IList?为什么我的 SelectList 不喜欢 IList 的内容?
【发布时间】:2021-01-27 23:25:24
【问题描述】:

这是我的 Razor 页面中的相关代码:我收到一条错误消息,指出对象(在选择元素行上)未设置为对象的实例。

<select asp-for="EmployeeLocation.EmployeeID" asp-items="@Model.EmployeeSelectList">

这是我的 PageModel 类中的相关代码:

public EmployeeLocation EmployeeLocation { get; set; }
public Employee Employee { get; set; }

public class JoinResult
{
    public int EmployeeID;
    public string LastName;
}
private IQueryable<JoinResult> JoinResultIQueryable;
private IList<JoinResult> JoinResultIList;
public SelectList EmployeeSelectList;

public async Task OnGetAsync()
{

    // Get the employee's locations.
    List<int> EmployeeLocationsList = HttpContext.Session.GetObject<List<int>>("EmployeeLocationsList");

    // Populate the employee select list.

    JoinResultIQueryable = (
        from e in IDDSContext.Employee
        join p in IDDSContext.Position on e.PositionID equals p.PositionID
        join el in IDDSContext.EmployeeLocation on e.EmployeeID equals el.EmployeeID
        where e.Active == true
            && e.PositionID != 1 // Do not display the super administrator's data.
            && EmployeeLocationsList.Contains(el.LocationID)
        select new JoinResult
        {
            EmployeeID = e.EmployeeID,
            LastName = e.LastName
        });

    JoinResultIList = await JoinResultIQueryable
        .Distinct()
        .OrderBy(jr => jr.LastName)
        .ToListAsync();

    // Display the results in the Output window. Just done to show contents of JoinResultIList (see below).
    foreach (var item in JoinResultIList)
    {
        Debug.WriteLine("[" + item.EmployeeID + "][" + item.LastName + "]");
    }

    // ***** The problem seem to occur here. Evidently, the SelectList doesn't like contents of the IList. Why is this happening?
    EmployeeSelectList = new SelectList(JoinResultIList, "EmployeeID", "LastName");

}

以下是 JoinResultIList 的内容(在输出窗口中):

[4][Anderson (OH)]
[30][Becon (OH)]
[26][Smith (OH)]
[25][Stevens (OH)]

【问题讨论】:

  • 嗨@Bob,根据您的代码,我使用您的代码创建了一个示例,一切正常,like this。我建议你可以尝试清除浏览器数据(缓存cookie等),然后检查代码是否运行良好。此外,您还可以使用 VS 2019 在此行中添加一个断点:&lt;select asp-for="EmployeeLocation.EmployeeID" asp-items="@Model.EmployeeSelectList" &gt;&lt;/select&gt;,然后调试您的代码并检查 EmployeeSelectList 是否包含项目。如果还是不行,可能是其他代码的问题,检查一下。
  • 嗨,@ZhiLv。我清除了缓存,但没有用。我还在你建议的地方设置了一个断点。它显示 SelectedValue = null、Items = 4、DataTextField = LastName 和 DataValueField = EmployeeID。这 4 个项目还具有正确的 EmployeeID 和 LastNames。 (我之前在 PageModel 类中设置了一个,它显示了同样的东西)。我将尝试您的代码并进行比较,看看是否有一些不同。
  • 我将我的 EmployeeSelectList (ESL) 与我称为 LocationSelectList (LSL) 的一个进行了比较,它运行良好。 ESL 需要 Employee 表和 EmployeeLocation 表之间的连接。 LSL 不需要连接,因此没有问题。在填充两个选择列表并将鼠标悬停在它们上方(我设置了一个断点)之后,我发现选择列表中的数据结构有所不同。要查看我需要访问的 Employee 表的属性,我必须向下钻取一层。如何设置它以便我可以直接访问两个表的数据?
  • 导致错误:EmployeeSelectList = new SelectList(IDDSContext.EmployeeLocation .Include(e => e.Employee) .Where(el => EmployeeLocationsList.Contains(el.LocationID)) .AsNoTracking() .OrderBy (el => el.Employee.LastName), "EmployeeID", "LastName");
  • 不会导致错误:LocationSelectList = new SelectList(IDDSContext.Location .Where(l => EmployeeLocationsList.Contains(l.LocationID)) .AsNoTracking() .OrderBy(l => l.Location1) , "LocationID", "Location1");

标签: list asp.net-core razor-pages selectlist


【解决方案1】:

好的。所以,这是我想出的解决方案。

// Get the employee's locations.
List<int> EmployeeLocationsList = HttpContext.Session.GetObject<List<int>>("EmployeeLocationsList");

// Populate the employee select list.
IQueryable JoinResultIQueryable = (
    from e in IDDSContext.Employee
    join p in IDDSContext.Position on e.PositionID equals p.PositionID
    join el in IDDSContext.EmployeeLocation on e.EmployeeID equals el.EmployeeID
    where e.Active == true
        && e.PositionID != 1 // Do not display the super administrator's data.
        && EmployeeLocationsList.Contains(el.LocationID)
    orderby e.LastName, e.FirstName
    select new
    {
        EmployeeID = e.EmployeeID,
        Employee = $"{e.LastName}, {e.FirstName} ({p.Position1})",
    });
EmployeeSelectList = new SelectList(JoinResultIQueryable, "EmployeeID", "Employee");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-25
    • 2015-08-06
    • 2011-04-03
    相关资源
    最近更新 更多