ASP.NET MVC将数据库值检索到Edit上的DropDownList

时间:2016-07-08 17:29:48

标签: c# asp.net asp.net-mvc

当我尝试“编辑”员工时,我无法检索我的部门和建筑物列表。我能够检索Employees First和Last Name,但是在Employees的Edit视图中我的DropDownList for Departments和Buildings是空的。请参阅下面的模型,控制器和视图。

员工模型:

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>

1 个答案:

答案 0 :(得分:0)

您的视图正在使用ViewBag,您忘记将项目设置为ViewBag.DeptListNameViewBag.BldgListName以呈现下拉列表。但是在您的编辑操作方法中,您没有设置它们(您在create方法中执行了此操作)。

public IActionResult Edit(int id)
{
   var emp = _context.Employees.Where(e => e.EmpId == id).FirstOrDefault();
   if(emp==null)
       return View("NotFound"); //make sure you have this view.

   ViewBag.DeptListName = GetDepartments();
   ViewBag.BldgListName = GetBuildings();
   return View(emp );
}