2013-02-26 107 views
0

我有兩個型號如下:.NET MVC下拉列表不更新

員工模型

public int ID{get;set;} 
public string name{get;set;} 
public virtual Department Department{get;set;} 

系車型

public int ID{get;set;} 
public string name{get;set;} 

而且我加入時使用的員工視圖中的下拉列表新員工

員工編輯控制器

public ActionResult Edit(employee employee) 
{ 
    .... 
    ViewBag.ID = new SelectList(db.departments,"ID","Name",employee.ID); 
    return View(employee); 
} 

在視圖:

@Html.DropDownList("ID") 

添加一個新員工妥善保存師,但是當我編輯現有的記錄,這是不節能。

我在忽略什麼?

型號Bindding:

using System; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using AssetManagement.Models; 

namespace AssetManagement.Controllers 
{ 
    public class EmployeeController : Controller 
    { 
     private AssetContext db = new AssetContext(); 

     // 
     // GET: /Employee/ 

     public ActionResult Index() 
     { 
      var employees = db.Employees.Include(e => e.Department); 
      return View(employees.ToList()); 
     } 

     // 
     // GET: /Employee/Details/5 

     public ActionResult Details(int id = 0) 
     { 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(employee); 
     } 

     // 
     // GET: /Employee/Create 

     public ActionResult Create() 
     { 
      ViewBag.ID = new SelectList(db.Departments, "ID", "Name"); 
      return View(); 
     } 

     // 
     // POST: /Employee/Create 

     [HttpPost] 
     public ActionResult Create(Employee employee) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Employees.Add(employee); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.ID = new SelectList(db.Departments, "ID", "Name", employee.ID); 
      return View(employee); 
     } 

     // 
     // GET: /Employee/Edit/5 

     public ActionResult Edit(int id = 0) 
     { 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      ViewBag.IDList = new SelectList(db.Departments, "ID", "Name", employee.ID); 
      return View(employee); 
     } 

     // 
     // POST: /Employee/Edit/5 

     [HttpPost] 
     public ActionResult Edit(Employee employee) 
     { 

      if (ModelState.IsValid) 
      { 
       db.Entry(employee).State = EntityState.Modified; 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 
      ViewBag.IDList = new SelectList(db.Departments, "ID", "Name", employee.ID); 

      return View(employee); 
     } 

     // 
     // GET: /Employee/Delete/5 

     public ActionResult Delete(int id = 0) 
     { 
      Employee employee = db.Employees.Find(id); 
      if (employee == null) 
      { 
       return HttpNotFound(); 
      } 
      return View(employee); 
     } 

     // 
     // POST: /Employee/Delete/5 

     [HttpPost, ActionName("Delete")] 
     public ActionResult DeleteConfirmed(int id) 
     { 
      Employee employee = db.Employees.Find(id); 
      db.Employees.Remove(employee); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      db.Dispose(); 
      base.Dispose(disposing); 
     } 
    } 
} 

公共類科{ 公衆詮釋ID {獲得;設置;} 公共字符串名稱{;組; } public virtual Employee SiteContact {get;組; } public ICollection Assets {get;組; }}

public class Employee 
{ 
    [Key, ForeignKey("Division")] 
    public int ID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public virtual Department Department{ get; set; } 
    public string Title { get; set; } 
    public string Address { get; set; } 
    public string City { get; set; } 
    public string BuildingName { get; set; } 
    public string Floor { get; set; } 
} 
+0

員工模型 – mpora 2013-02-26 20:02:40

+0

好吧,我有點糊塗了B/C似乎要綁定部門列表中的Emp ID,但這裏是我最好的迴應的理解。 – 2013-02-26 20:04:53

+0

那麼我有一個部門的集合,我想在添加新員工時能夠選擇一個部門。 – mpora 2013-02-26 20:14:06

回答

0

實體框架不保存,因爲僱員實體「僱員」(在編輯帖子帕拉姆)不在範圍內。當再次調用控制器時,前一個上下文將被銷燬,並且此實體不在實際上下文中。 Get和Post中的db上下文不一樣。

因此,您必須去搜索實體進行編輯和修改。之後,你可以保存你的實體。

試試這個:

[HttpPost] 
    public ActionResult Edit(Employee employee) 
    { 

     if (ModelState.IsValid) 
     { 
      var employeeModify = db.Find(employee.Id); 
      employeeModify.name = employee.Name; 
      employeeModify.DepartamentId = employee.DepartamentId; 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 
     ViewBag.IDList = new SelectList(db.Departments, "ID", "Name", employee.ID); 

     return View(employee); 
    }