public ActionResult Edit(int?id, int?contactid, int?projectid)
        {
            ProjectPunchLists _punchList = db.ProjectPunchLists.Where(p => p.ProjectPunchListID == (id ?? 0)).FirstOrDefault();

            if (_punchList == null)
            {
                return(HttpNotFound());
            }

            if (!contactid.HasValue && projectid.HasValue)
            {
                contactid = new BOL.Repository.CommonRepository().GetContactIdByProjectID(projectid.Value);
            }

            var jobs = db.GetJobsByContactID_WithLockFalse(contactid).ToList();

            ViewBag.Jobs = new SelectList(jobs, "ViewID", "Project", _punchList.ProjectID);

            ViewBag.Types       = new SelectList(repo.GetPunchListTypesBySiteCoID(siteusercompanyid), "PunchListTypeID", "PunchListType");
            ViewBag.Departments = new SelectList(repo.GetPunchListDepartmentsBySiteCoID(siteusercompanyid), "PunchItemDepartmentID", "ItemDepartment");
            ViewBag.Priorities  = new SelectList(repo.GetPrioritiesList(), "Item1", "Item2");
            ViewBag.Status      = new SelectList(repo.GetActivityStatus(siteusercompanyid), "StatusID", "StatusName");

            ViewBag.Divisions = new SelectList(repo.GetDivisionsByProjectID(_punchList.ProjectID ?? 0), "ProjectDivisionID", "DivisionName");
            ViewBag.Areas     = new SelectList(repo.GetAreasByProjectID(_punchList.ProjectID ?? 0), "ProjectAreaID", "AreaName");

            var siteUsers = db.SiteUsers.Where(p => p.SiteCoID == siteusercompanyid).OrderBy(p => p.UserDisplayName);

            ViewBag.Creator = new SelectList(siteUsers, "SiteUserID", "UserDisplayName", siteuserid);

            PunchListModels.NewPunchList model = new PunchListModels.NewPunchList();

            model = new PunchListModels.NewPunchList()
            {
                PunchListID  = _punchList.ProjectPunchListID,
                AreaID       = _punchList.AreaID,
                CreatorID    = _punchList.CreatedByUserID,
                DepartmentID = _punchList.DepartmentID,
                Description  = _punchList.Subject,
                DivisionID   = _punchList.DivisionID,
                Due          = _punchList.DueDate,
                Hours        = _punchList.EstHrs,
                JobID        = _punchList.PriorityID,
                Notes        = _punchList.Note,
                PriorityID   = _punchList.PriorityID,
                StatusID     = _punchList.StatusID,
                TypeID       = _punchList.TypeID
            };

            return(View("_Edit", model));
        }
        private int SavePunchList(PunchListModels.NewPunchList Model)
        {
            int siteCoID = siteusercompanyid;

            ProjectPunchLists newPunchList = new ProjectPunchLists()
            {
                ProjectPunchListID = Model.PunchListID ?? 0,
                SiteCoID           = siteusercompanyid,
                ProjectID          = Model.JobID,
                Subject            = Model.Description,
                TypeID             = Model.TypeID,
                PriorityID         = Model.PriorityID,
                StatusID           = Model.StatusID,
                CreatedByUserID    = Model.CreatorID,
                DepartmentID       = Model.DepartmentID,
                DivisionID         = Model.DivisionID,
                AreaID             = Model.AreaID,
                Note    = Model.Notes,
                EstHrs  = Model.Hours,
                DueDate = Model.Due
            };

            if (newPunchList.ProjectPunchListID == 0)
            {
                newPunchList.CreateDate = DateTime.Now;
            }

            int punchListID = repo.SavePunchList(newPunchList);

            if (punchListID > 0)
            {
                return(punchListID);
            }
            else
            {
                return(0);
            }
        }
        public ActionResult Create(PunchListModels.NewPunchList Model)
        {
            var errorList = new List <string>();

            if (ModelState.IsValid)
            {
                int punchListID = SavePunchList(Model);
                if (punchListID > 0)
                {
                    return(Json(new { status = "success", punchListID }));
                }
                else
                {
                    errorList.Add("Punch item couldn't be saved. Please retry.");
                }
            }

            errorList.AddRange((from item in ModelState.Values
                                from error in item.Errors
                                select error.ErrorMessage).ToList()
                               );

            return(Json(new { status = "error", errors = errorList }));
        }