我在嘗試「編輯」員工時檢索我的部門和建築物列表時遇到問題。我能夠檢索員工姓名和姓氏,但是編輯員工視圖的部門和大樓的DropDownList爲空。請參閱下面的模型,控制器和視圖。ASP.NET MVC在編輯時將數據庫值檢索到DropDownList
員工型號:
public class Employee
{
[Key]
public int EmpId { get; set; }
[Required]
public string EmpFirstName { get; set; }
[Required]
public string EmpLastName { get; set; }
public int DeptId { get; set; }
public Department Department { get; set; }
public int BldgId { get; set; }
public Building Building { get; set; }
}
EmployeeController:
namespace DataEntryMVCEFCore.Controllers
{
public class EmployeeController : Controller
{
private DataEntryContext _context;
public EmployeeController(DataEntryContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Employees.ToList());
}
// Populate Department values to DropDownList
private IEnumerable<SelectListItem> GetDepartments()
{
return _context.Departments
.Select(s => new SelectListItem
{
Value = s.DeptId.ToString(),
Text = s.DeptTitle
})
.ToList();
}
// Populate Building values to DropDownList
private IEnumerable<SelectListItem> GetBuildings()
{
return _context.Buildings
.Select(s => new SelectListItem
{
Value = s.BldgId.ToString(),
Text = s.BldgName
})
.ToList();
}
public IActionResult Create()
{
// Load values to DropDownLists for Departments and Buildings for Create Method
ViewBag.DeptListName = GetDepartments();
ViewBag.BldgListName = GetBuildings();
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
_context.Employees.Add(employee);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
public IActionResult Edit(int id)
{
var Employee = _context.Employees
.Where(e => e.EmpId == id)
.Single();
return View(Employee);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
_context.Employees.Update(employee);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
}
}
員工創建視圖:
<form asp-controller="employee" asp-action="Create" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpFirstName" class="form-control" />
<span asp-validation-for="EmpFirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="EmpLastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpLastName" class="form-control" />
<span asp-validation-for="EmpLastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="DeptId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control">
<option>Please Select</option>
</select>
<span asp-validation-for="DeptId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BldgId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control">
<option>Please Select</option>
</select>
<span asp-validation-for="BldgId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
員工編輯觀點:
<form asp-controller="employee" asp-action="Edit" method="post" class="form-horizontal" role="form">
<div class="form-horizontal">
<div asp-validation-summary="All" class="text-danger"></div>
<div class="form-group">
<label asp-for="EmpFirstName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpFirstName" class="form-control" />
<span asp-validation-for="EmpFirstName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="EmpLastName" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="EmpLastName" class="form-control" />
<span asp-validation-for="EmpLastName" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="DeptId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("DeptList", ViewBag.DeptListName as SelectList, "Select Department")*@
<select asp-for="DeptId" asp-items="@ViewBag.DeptListName" class="form-control"></select>
<span asp-validation-for="DeptId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="BldgId" class="col-md-2 control-label"></label>
<div class="col-md-10">
@*@Html.DropDownList("BldgList", ViewBag.BldgListName as SelectList, "Select Building")*@
<select asp-for="BldgId" asp-items="@ViewBag.BldgListName" class="form-control"></select>
<span asp-validation-for="BldgId" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
http://stackoverflow.com/questions/18382311/populating-a-razor-dropdownlist-from-a-listobject-in-mvc – Larry