2016-07-08 53 views
0

我在嘗試「編輯」員工時檢索我的部門和建築物列表時遇到問題。我能夠檢索員工姓名和姓氏,但是編輯員工視圖的部門和大樓的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> 
+0

http://stackoverflow.com/questions/18382311/populating-a-razor-dropdownlist-from-a-listobject-in-mvc – Larry

回答

0

您的視圖是使用ViewBag,你忘了設置的項目ViewBag.DeptListNameViewBag.BldgListName呈現下拉菜單。但是在你的編輯動作方法中,你並沒有設置這些(你在創建方法中做過)。

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); 
} 
+0

嗨Shyju, 感謝您的回覆回來..我實現你提到了什麼,我能夠將數據檢索到DropDownLists以及部門和建築物的DropDownLists中適當的選定值。 但是,當我嘗試更新DropDownList值甚至是名字和姓氏時,我在下面得到以下錯誤: – Brian

+0

Microsoft.EntityFrameworkCore.dll中發生類型爲「Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException」的異常,但未處理用戶代碼 附加信息:數據庫操作預計影響1行,但實際上影響0行。自實體加載後,數據可能已被修改或刪除。有關理解和處理樂觀併發異常的信息,請參閱http://go.microsoft.com/fwlink/?LinkId=527962。 – Brian

+0

該錯誤與您的原始問題或我爲此提供的答案無關。閱讀例外的細節,你應該能夠弄清楚。 – Shyju

相關問題