public ActionResult Create(long Person_Id, long Department_Id)
 {
     var model = new PersonDepartmentModel { Selected = new PersonDepartment { Person_Id = Person_Id, Department_Id = Department_Id } };
     using (var db = new NespeDbContext()) {
         Bind(model.Selected, db);
     }
     return View(model);
 }
 public ActionResult AddDepartment(long Id, long Department_Id)
 {
     using (var db = new NespeDbContext())
     {
         var drc = db.PersonDepartmentSet;
         var dr = Get(Id, db);
         if (dr == null)
         {
             base.ModelState.AddModelError("Action.Details.Invalid.Id", "Invalid Id");
             return RedirectToAction("Index");
         }
         var model = new PersonDepartmentModel { Selected = dr };
         Bind(model.Selected, db);
         return View(model);
     }
 }
        public ActionResult AddDepartment(PersonDepartmentModel model, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                using (var db = new NespeDbContext())
                {
                    var drc = db.PersonDepartmentSet;
                    var dr = (from t in drc where t.Id == model.Selected.Id select t).FirstOrDefault();
                    if (dr == null)
                    {
                        base.ModelState.AddModelError("Action.Details.Invalid.Id", "Invalid Id");
                        return RedirectToAction("Index");
                    }
                    model.Selected = dr;
                }

            }
            return View(model);
        }
 public ActionResult Create(PersonDepartmentModel model, FormCollection formCollection)
 {
     if (ModelState.IsValid)
     {
         using (var db = new NespeDbContext())
         {
             var drc = db.PersonDepartmentSet;
             var selected = model.Selected;
             var dr = (from t in drc where t.Person_Id == selected.Person_Id && t.Department_Id == selected.Department_Id select t).FirstOrDefault();
             if (dr != null && dr.Id > 0)
             {
                 return RedirectToAction("Edit", new { Id = dr.Id });
             }
             else
             {
                 Bind(selected, db);
                 drc.Add(selected);
             }
             db.SaveChanges();
         }
         return RedirectToAction("Index");
     }
     return View(model);
 }
        //
        // GET: /Person/
        public ActionResult Index()
        {
            using (var db = new NespeDbContext())
            {
                var drc = db.PersonDepartmentSet;
                var model = new PersonDepartmentModel { };

                model.Items = Query(db).ToList();
                return View(model);
            }
        }
        public ActionResult Edit(PersonDepartmentModel model, FormCollection formCollection)
        {
            var selected = model.Selected;
            using (var db = new NespeDbContext())
            {
                Bind(selected, db);

                if (ModelState.IsValid)
                {

                    var drc = db.PersonDepartmentSet;
                    //drc.Attach(model.Selected);
                    var dr = Get(model.Selected.Id, db);
                    if (dr != null)
                    {
                        dr.Copy(selected);
                        selected = dr;
                    }
                    else
                        drc.Add(selected);

                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            return View(model);
        }
        public ActionResult Details(PersonDepartmentModel model, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                using (var db = new NespeDbContext())
                {
                    var drc = db.PersonDepartmentSet;
                    var dr = Get(model.Selected.Id, db);
                    if (dr == null)
                    {
                        base.ModelState.AddModelError("Action.Details.Invalid.Id", "Invalid Id");
                        return RedirectToAction("Index");
                    }
                    model.Selected = dr;
                }

            }
            return View(model);
        }