コード例 #1
0
        public ActionResult SavePeople([Bind(Include = "Id,LastName,FirstName,BirthDate,PhotoPeople,ImageIsEmpty")] PeopleViewModel peopleModel)
        {
            bool saveCreateMode = (peopleModel.Id == 0) ? true : false;

            if (ModelState.IsValid)
            {
                try
                {
                    // проверка на ввод Даты рождения (возраст от 5 до 150 лет)
                    if (!UtilHelper.CheckBirthDate(peopleModel.BirthDate))
                    {
                        ModelState.AddModelError("BirthDate", "Возраст может быть от 5 до 150 лет! Введите корректную дату рождения");
                        return(View("CreateEditPeople", peopleModel));
                    }

                    // если изменяем запись
                    if (!saveCreateMode && peopleModel.PhotoPeople == null)
                    {
                        // если фото было удалено пользователем
                        if (peopleModel.ImageIsEmpty)
                        {
                            peopleModel.PhotoPeople   = null;
                            peopleModel.PhotoMIMEType = "";
                        }
                        else
                        {
                            PeopleViewModel tmpPeopleModel = repository.GetPeople(peopleModel.Id);
                            peopleModel.PhotoPeople   = tmpPeopleModel.PhotoPeople;
                            peopleModel.PhotoMIMEType = tmpPeopleModel.PhotoMIMEType;
                        }
                    }
                    else
                    {
                        if (peopleModel.PhotoPeople != null && peopleModel.PhotoPeople.ContentLength > 0)
                        {
                            peopleModel.PhotoMIMEType = peopleModel.PhotoPeople.ContentType;
                        }
                        else
                        {
                            peopleModel.PhotoMIMEType = "";
                        }
                    }

                    repository.SavePeople(peopleModel);
                    if (saveCreateMode)
                    {
                        Logger.logger.Info(String.Format("Добавлен человек:\n LastName={0}, FirstName={1}, BirthDate={2}",
                                                         peopleModel.LastName, peopleModel.FirstName, peopleModel.BirthDateStr));
                    }
                    else
                    {
                        Logger.logger.Info(String.Format("Изменен человек:\n Id={0}, LastName={1}, FirstName={2}, BirthDate={3}",
                                                         peopleModel.Id, peopleModel.LastName, peopleModel.FirstName, peopleModel.BirthDateStr));
                    }

                    return(RedirectToAction("Index"));
                }
                catch (Exception e)
                {
                    Logger.LogException(e);
                }
            }
            else
            {
                return(View("CreateEditPeople", peopleModel));
            }

            return(RedirectToAction("CreateEditPeople", peopleModel.Id));
        }