Пример #1
0
        public ActionResult Create(LocationFormViewModel model)
        {
            LocationDisplaySetting locationDisplaySetting = new LocationDisplaySetting();

            if (ViewData.ModelState.IsValid)
            {
                model.UserId = UserService.GetUser(User.Identity.Name).UserID;
                var location = model.getLocationFromViewModel();

                if (model.CurrentDisplaySetting)
                {
                    if (string.IsNullOrWhiteSpace(model.DisplayName))
                    {
                        ModelState.AddModelError("", "Мора да внесете име на новиот поглед");
                        PopulateAddLocationViewModel(model);
                        return(View(model));
                    }

                    locationDisplaySetting = model.getLocationDisplaySettingFromViewModel();
                    var resultDisplay = LocationService.AddLocationDisplaySetting(locationDisplaySetting);

                    if (!resultDisplay.IsOK)
                    {
                        PopulateAddLocationViewModel(model);

                        foreach (var message in resultDisplay.Messages.Union(resultDisplay.ExceptionMessages))
                        {
                            ModelState.AddModelError("", message);
                        }

                        return(View(model));
                    }

                    location.DisplaySettings = locationDisplaySetting.ID;
                }

                var result = LocationService.AddLocation(location);

                if (result.IsOK)
                {
                    return(RedirectToAction("Details", new { id = location.ID }));
                }
                else
                {
                    PopulateAddLocationViewModel(model);

                    foreach (var message in result.Messages.Union(result.ExceptionMessages))
                    {
                        ModelState.AddModelError("", message);
                    }

                    return(View(model));
                }
            }
            else
            {
                PopulateAddLocationViewModel(model);
                return(View(model));
            }
        }
Пример #2
0
        public ActionResult Edit(int id, LocationFormViewModel model)
        {
            LocationDisplaySetting locationDisplaySetting = new LocationDisplaySetting();

            if (ViewData.ModelState.IsValid)
            {
                var location = model.getLocationFromViewModel();

                if (model.CurrentDisplaySetting)
                {
                    //Check the name
                    //TODO : This needs to go somepalce else. The Check needs to be done some other way. not in the controller
                    //TODO : again use the damn ViewModel to validate. usint the supplied attributes for data validation
                    if (string.IsNullOrWhiteSpace(model.DisplayName))
                    {
                        ModelState.AddModelError("", "Мора да внесете име на новиот поглед");
                        PopulateAddLocationViewModel(model);
                        return(View(model));
                    }

                    locationDisplaySetting = model.getLocationDisplaySettingFromViewModel();
                    var resultDisplay = LocationService.AddLocationDisplaySetting(locationDisplaySetting);

                    if (!resultDisplay.IsOK)
                    {
                        PopulateAddLocationViewModel(model);
                        foreach (var message in resultDisplay.Messages.Union(resultDisplay.ExceptionMessages))
                        {
                            ModelState.AddModelError("", message);
                        }
                        return(View(model));
                    }

                    location.DisplaySettings = locationDisplaySetting.ID;
                }

                var result = LocationService.UpdateLocation(location);

                if (result.IsOK)
                {
                    return(RedirectToAction("Details", new { id = location.ID }));
                }
                else
                {
                    PopulateAddLocationViewModel(model);

                    foreach (var message in result.Messages.Union(result.ExceptionMessages))
                    {
                        ModelState.AddModelError("", message);
                    }

                    return(View(model));
                }
            }
            else
            {
                PopulateAddLocationViewModel(model);
                return(View(model));
            }
        }