【发布时间】:2023-03-19 11:30:02
【问题描述】:
首先,我知道这个问题已经被问过很多次了。我已经阅读了无数的文章和 Stack Overflow 的答案。我已经尝试了四天来解决这个问题,如果有人不介意,我想我需要帮助。
我有两个数据库。员工数据库有一个名为“DisplayName”的字段——第二个数据库与第一个数据库有关系,它们一起工作得很好。我可以在另一个应用程序中完美地调用这两个数据库。
你可以看到图片中的Index Page 我有一份人员名单。我想要一个下拉列表,列出数据库中的所有显示名称,以便员工可以将自己添加到列表中。您会在图像中看到一个下拉列表,但它没有被填充。
看起来很简单。但是,天哪。我遇到的部分问题是我的家庭控制器已经有一个功能来填充图片中的列表,所以我不能在那个页面上做另一个。我在很多网站上尝试了很多建议。我收到 IEnumerable 错误或显示引用错误....
这是我的控制器(同样 - 它没有任何帮助下拉菜单的内容):
namespace SeatingChart.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Employee
public ActionResult Index()
{
var lists = db.BreakModels
.Include("Employee")
.Include("TimeEntered")
.Include("TimeCleared")
.Include("DisplayName")
.Select(a => new HomeIndexViewModels
{
Employee = a.Employee,
DisplayName = a.EmployeeModels.DisplayName,
TimeEntered = a.TimeEntered,
TimeCleared = a.TimeCleared.Value,
Id = a.EmployeeModels.Id,
});
return View(lists);
}
查看:
@model IEnumerable<SeatingChart.Models.HomeIndexViewModels>
@{
Layout = null;
}
@Html.Partial("_Header")
<div class="container_lists">
<div class="container_break col-md-8">
<h5 style="text-align:center">Break List</h5>
<table class="table-bordered col-lg-12">
@if (Model != null)
{
foreach (var item in Model)
{
if (item.TimeCleared == null)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
 BV
</td>
<td>
 @item.TimeEntered.ToString("HH:mm")
</td>
</tr>
}
}
}
</table>
@using (Html.BeginForm())
{
<div class="row site-spaced">
<div class="col-3">
@Html.DropDownList("DisplayName", new SelectList(new List<string>() { "---Dispatcher---" }), new { @class = "required " })
</div>
</div>
<div class="col-3">
<input type="submit" value="Submit" class="site-control" />
</div>
}
</div>
</div>
视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace SeatingChart.Models
{
public class HomeIndexViewModels
{
//Break Model
public int BreakId { get; set; }
public int Employee { get; set; }
public DateTime TimeEntered { get; set; }
public DateTime? TimeCleared { get; set; }
//Employee Model
public int Id { get; set; }
public string DisplayName { get; set; }
public string DisplayNames { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool NotActive { get; set; }
public int Force { get; set; }
public string EmployeeList { get; set; }
}
}
我希望这已经足够清楚了。我用这么多代码尝试了这么多不同的方法——我尝试过的所有错误都是不同的。
提前感谢您的耐心和帮助!
【问题讨论】:
标签: asp.net-mvc dropdown