コード例 #1
0
        public async Task <ActionResult> CreateCountrySetting(CountrySettingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View("Error"));
            }

            var countryId = model.CountryId ?? 0;

            if (countryId == 0)
            {
                ViewBag.Error = @Resources.ErrorMessages.NoCountryId;
                return(View("Error"));
            }

            var setting = _countrySettingsService.GetItem(countryId);

            if (setting != null)
            {
                ViewBag.Error = @Resources.ErrorMessages.SettingAlreadyExists;
                return(View("Error"));
            }

            try
            {
                var item = Mapper.Map <CountrySettingViewModel, CountrySettingDTO>(model);

                _countrySettingsService.AddItem(item);
                _countrySettingsService.Commit();
                _countrySettingsService.Dispose();

                var userName = User.Identity.Name;
                Logger.Log.Info($"{userName}: CreateCountrySetting {model.CountryId} ");

                TempData["Success"] = Resources.Messages.SettingsCreateSuccess;
                return(RedirectToAction("CountrySettings"));
            }
            catch (Exception ex)
            {
                var userName = User.Identity.Name;
                Logger.Log.Error($"{userName}: CreateProduct() {ex.Message} ");

                @ViewBag.Error = ex.Message;
                return(View("Error"));
            }
        }