0
votes

I have two models as follows:

employee model

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

Department model

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

And I am using a dropdown on the employee view when adding a new employee

employee edit controller

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

In the view:

@Html.DropDownList("ID")

Adding a new employee saves the division properly but when I edit an existing record, it is not saving.

What am I overlooking?

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

public class Division { public int ID {get;set;} public string Name { get; set; } public virtual Employee SiteContact { get; set; } public ICollection Assets{ get; set; } }

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; }
}
1
ok, I'm a little confused b/c it seems you're binding dept list to Emp ID, but here is my best understanding of your response.Dave Alperovich
Well I have a collection of departments and I would like to be able to select a department when adding a new employee.mpora
ok, i see your controller. But still, which action are you calling, and what model are you binding in your view?Dave Alperovich
I have added the employee modelmpora
Still trying things, I will get back to you in an hour or sompora

1 Answers

0
votes

Entity Framework not saving because the Employee entity "employee" (the param in Edit Post) is not in the context. When you call again to the controller, the previous context is destroyed, and this Entity is not in the actual context. The db Context in the Get and in the Post is not the same.

Therefore, you must go to search the entity to edit and modified this. After that you can save your entity.

Try this:

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