0

我有两个模型如下:

员工模型

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")

添加新员工可以正确保存部门,但是当我编辑现有记录时,它没有保存。

我在看什么?

模型绑定:

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

public class Division { public int ID {get;set;} public string Name { get; 放; } 公共虚拟员工 SiteContact { 获取;放; } 公共 ICollection 资产{ 获取;放; } }

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; }
}
4

1 回答 1

0

实体框架未保存,因为 Employee 实体“员工”(编辑帖子中的参数)不在上下文中。当你再次调用控制器时,之前的上下文被破坏了,这个实体不在实际的上下文中。Get 和 Post 中的 db Context 不一样。

因此,您必须去搜索实体进行编辑和修改。之后,您可以保存您的实体。

尝试这个:

    [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);
    }
于 2015-07-04T17:11:44.487 回答