public ActionResult Edit(int?id)
        {
            if (!id.HasValue)
            {
                return(StatusCode(StatusCodes.Status400BadRequest));
            }

            var projectScheduleEntryType = _projectScheduleEntryTypeService.GetById(id.Value);

            if (projectScheduleEntryType == null)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            ViewBag.ProjectTypeID = new SelectList(_projectTypeService.Get(x => x.ToList().OrderBy(pt => pt.FullName).ToList()), "ID", "FullName", projectScheduleEntryType.ProjectTypeID);

            return(View(projectScheduleEntryType));
        }
Beispiel #2
0
        public ActionResult Create(ProjectScheduleEntry projectScheduleEntry, string modelTitle)
        {
            string shortNameFromList = "ShortNameFromList";

            ModelState.Clear();

            var  shortNameEditMode   = Request.Query["shortNameEditMode"].ToString();
            bool isShortNameFromList = shortNameEditMode.Equals(shortNameFromList, StringComparison.OrdinalIgnoreCase);

            string allowDuplicateScheduleEntryValue = Request.Query["AllowDuplicateScheduleEntry"].ToString();
            bool   allowDuplicateScheduleEntry      = string.IsNullOrEmpty(allowDuplicateScheduleEntryValue) ? false : allowDuplicateScheduleEntryValue.Contains("true");

            if (isShortNameFromList)
            {
                if (projectScheduleEntry.ProjectScheduleEntryTypeID.HasValue)
                {
                    projectScheduleEntry.Title = _projectScheduleEntryTypeService.GetById(projectScheduleEntry.ProjectScheduleEntryTypeID.Value).Title;
                }
                else
                {
                    ModelState.AddModelError("ProjectScheduleEntryTypeID", "Требуется поле Значение из справочника.");
                }

                if (projectScheduleEntry.ProjectScheduleEntryTypeID.HasValue)
                {
                    var result = _projectScheduleEntryService.Get(entries => entries
                                                                  .Where(pse => pse.ProjectScheduleEntryTypeID.HasValue && pse.ProjectScheduleEntryTypeID.Value == projectScheduleEntry.ProjectScheduleEntryTypeID.Value &&
                                                                         pse.ProjectID == projectScheduleEntry.ProjectID).ToList());
                    int count = result.Count();
                    if (count > 0 && !allowDuplicateScheduleEntry)
                    {
                        ModelState.AddModelError("AllowDuplicateScheduleEntry", "Данный тип вехи уже был использован, необходимо разрешить повторное создание вехи.");
                    }
                    else if (count > 0)
                    {
                        int num = count + 1;
                        projectScheduleEntry.Title += $" ({num})";
                    }
                }
                ModelState.Remove("Title");
            }
            else
            {
                projectScheduleEntry.Title = modelTitle;
                ViewBag.ModelTitle         = modelTitle;
                if (!string.IsNullOrEmpty(modelTitle))
                {
                    string trimmedValue = modelTitle.Trim();
                    IList <ProjectScheduleEntry> resultEntries = null;
                    resultEntries = _projectScheduleEntryService.Get(entries => entries.Where(entry =>
                                                                                              entry.ProjectID == projectScheduleEntry.ProjectID &&
                                                                                              entry.Title.Equals(trimmedValue, StringComparison.OrdinalIgnoreCase) ||
                                                                                              (entry.ProjectScheduleEntryType != null && entry.ProjectScheduleEntryType.Title.Equals(trimmedValue, StringComparison.OrdinalIgnoreCase))).ToList());

                    int count = resultEntries.Count();
                    if (count > 0)
                    {
                        ModelState.AddModelError("Title", "Пользователь должен указать уникальное для данного проекта название вехи.");
                    }
                }
            }

            this.TryValidateModel(projectScheduleEntry);
            if (isShortNameFromList)
            {
                ModelState.Remove("Title");
            }

            if (ModelState.IsValid)
            {
                _projectScheduleEntryService.Add(projectScheduleEntry);
                string returnUrl = Url.Action("Details", "Project", new { id = projectScheduleEntry.ProjectID + "#scheduleentries" }).Replace("%23", "#");
                return(new RedirectResult(returnUrl));
            }

            ViewBag.ShortNameFromList           = isShortNameFromList;
            ViewBag.AllowDuplicateScheduleEntry = allowDuplicateScheduleEntry;
            var project = _projectService.GetById(projectScheduleEntry.ProjectID);

            ViewBag.ProjectId = project.ID;

            if (project != null)
            {
                int projectid = project.ID;
                ViewBag.Projects = new SelectList(_projectService.GetAll(null, null, null, ProjectStatus.All, null).Where(p => p.ID == projectid), "ID", "ShortName");

                ViewBag.ProjectScheduleEntryTypeID = new SelectList(_projectScheduleEntryTypeService.Get(psetList => psetList
                                                                                                         .Where(pset => pset.ProjectTypeID == project.ProjectTypeID || pset.ProjectTypeID == null).ToList()
                                                                                                         .OrderBy(pset => pset.WBSCode).ToList()), "ID", "WBSCodeName");
            }
            else
            {
                ViewBag.Projects = new SelectList(_projectService.GetAll(null, null, null, ProjectStatus.All, null), "ID", "ShortName", projectScheduleEntry.ProjectID);
                ViewBag.ProjectScheduleEntryTypeID = new SelectList(_projectScheduleEntryTypeService.Get(psetList => psetList.OrderBy(pset => pset.WBSCode).ToList()), "ID", "WBSCodeName", projectScheduleEntry.ProjectScheduleEntryTypeID);
            }
            return(View(projectScheduleEntry));
        }