public ActionResult Add(FarmCropViewModel model) { //SaveCrop(model.FarmId, model.CropId); if (ModelState.IsValid) { if (model.Id > 0) { FarmCrop farmCropToUpdate = FarmCropService.GetById(model.Id); farmCropToUpdate.Hectarage = model.Hectarage; farmCropToUpdate.YieldPerHectar = model.YieldPerHectar; farmCropToUpdate.CropVarietyNote = model.CropVarietyNote; farmCropToUpdate.IsActive = true; FarmCropService.Update(farmCropToUpdate); return(RedirectToAction("Index")); } var farmCrop = new FarmCrop { FarmId = model.FarmId, CropVarietyId = model.CropVarietyId, Hectarage = model.Hectarage, YieldPerHectar = model.YieldPerHectar, CropVarietyNote = model.CropVarietyNote, IsActive = true }; FarmCropService.Create(farmCrop); return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }
public ActionResult GetFarmCropList(int?farmId) { var model = new FarmCropViewModel { FarmCropList = FarmCropService.Get().Where(x => x.FarmId == farmId.Value && x.Farm.IsActive).ToList(), FarmId = farmId.Value }; return(PartialView("_FarmCropList", model)); }
//public ActionResult CropHarvest(int farmCropId) //{ // Planting _cropharvest = null; // var all = PlantingService.Get().Where(x => x.FarmCropId.Value == farmCropId).ToList(); // if (all.Any()) // { // _cropharvest = all.LastOrDefault(); // } // var model = new FarmCropViewModel // { // Harvest = _cropharvest // }; // //return PartialView("_CropHarvest", model); // return View(model); //} // GET: Crop/Create public ActionResult Add(int?farmId) { var model = new FarmCropViewModel { CropTypeDropDown = GetCropType(null), CropDropDown = GetCropEmpty(null), CropVarietyDropDown = GetCropVariety(null), FarmId = farmId.Value }; return(View(model)); }
//public ActionResult CropHarvest(int farmCropId) //{ // Planting _cropharvest = null; // var all = PlantingService.Get().Where(x => x.FarmCropId.Value == farmCropId).ToList(); // if (all.Any()) // { // _cropharvest = all.LastOrDefault(); // } // var model = new FarmCropViewModel // { // Harvest = _cropharvest // }; // //return PartialView("_CropHarvest", model); // return View(model); //} // GET: Crop/Create public ActionResult AddFarmCrop(int?farmId) { var model = new FarmCropViewModel { CropTypeDropDown = GetCropType(null), CropDropDown = GetCropEmpty(null), CropVarietyDropDown = GetCropVariety(null), FarmId = farmId.Value }; return(PartialView("_AddCropToFarmDialog", model)); }
// GET: Crop public ActionResult Index(int?farmId) { string userId = User.Identity.GetUserId(); var model = new FarmCropViewModel { FarmCropList = FarmCropService.Get().Where(x => x.Farm.ApplicationUserId == userId).ToList(), //FarmDropDown = base.GetMyFarm(null) CropDropDown = GetCrop(null), CropTypeDropDown = GetCropType(null), CropVarietyDropDown = GetCropVariety(null) }; return(View(model)); }
// GET: Crop/Edit/5 public PartialViewResult EditCrop(int FarmCropId) { FarmCrop farmCrop = FarmCropService.GetById(FarmCropId); FarmCropViewModel model = new FarmCropViewModel { Id = farmCrop.Id, CropId = farmCrop.CropVariety.Crop.Id, CropVarietyId = farmCrop.CropVariety.Crop.Id, CropTypeId = (int)farmCrop.CropVariety.Crop.CropTypeId, CropDropDown = GetCrop(farmCrop.CropVariety.Crop.Id), CropVarietyDropDown = GetCropVariety(farmCrop.CropVarietyId), CropTypeDropDown = GetCropType(farmCrop.CropVariety.Crop.CropTypeId) }; return(PartialView("_AddCropToFarmDialog", model)); }
public ActionResult Create(FarmCropViewModel model) { var cache = MemoryCache.Default; //если есть в кэше - берём из него, в противном случае запрашиваем из IFarmService var rc = (cache.Get("regions") as IEnumerable <NamedItemViewModel>) ?? farmService.GetRegions().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); var fc = (cache.Get("farmers") as IEnumerable <NamedItemViewModel>) ?? farmService.GetFarmers().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); var ac = cache.Get("agricultures") as IEnumerable <NamedItemViewModel> ?? farmService.GetAgricultures().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); //TODO: использовать полноценный тип для View ViewBag.Regions = rc; ViewBag.Farmers = fc; ViewBag.Agricultures = ac; if (model.Area <= 0) { ModelState.AddModelError("Area", "Площадь должна быть больше нуля"); } if (model.Gather < 0) { ModelState.AddModelError("Gather", "Урожай не может быть меньше нуля"); } if (!ModelState.IsValid) { return(View(model)); } try { var farmCrop = mapper.Map <FarmCropDto>(model); farmService.AddFarmCrop(farmCrop); } catch (ValidationException ex) { ModelState.AddModelError(ex.Property, ex.Message); return(View(model)); } return(RedirectToAction("List")); }
public ActionResult Create(FarmCropViewModel model) { /* * CR-1 - add check argument for null */ /* * CR-1 * Reduce code duplication with previous method, use ValueProvider class from comments above * Add cached values only in case of redirection back to create form because of failed validation */ var cache = MemoryCache.Default; //если есть в кэше - берём из него, в противном случае запрашиваем из IFarmService var rc = (cache.Get("regions") as IEnumerable <NamedItemViewModel>) ?? farmService.GetRegions().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); var fc = (cache.Get("farmers") as IEnumerable <NamedItemViewModel>) ?? farmService.GetFarmers().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); var ac = cache.Get("agricultures") as IEnumerable <NamedItemViewModel> ?? farmService.GetAgricultures().AsQueryable().ProjectTo <NamedItemViewModel>(mapper.ConfigurationProvider); ViewBag.Regions = rc; ViewBag.Farmers = fc; ViewBag.Agricultures = ac; /* * CR-1 * Gather all validation rules into one place - class FarmCropViewModel, let it validate its data, * Implement method FarmCropViewModel.Validate that returns dictionary with validation errors instead of exceptions * Check validation results here in controller. If there are no errors, save farm to DB. * If there are an errors, add them to ModelState and redirect back to form */ /* * CR-1 - change condition from "<=" to "<". */ if (model.Area <= 0) { ModelState.AddModelError("Area", "Площадь должна быть больше нуля"); } if (model.Gather < 0) { ModelState.AddModelError("Gather", "Урожай не может быть меньше нуля"); } if (!ModelState.IsValid) { /* * CR-1 - add regions, farmers and agricultures only here */ return(View(model)); } /* * CR-1 - get rid of exception based logic, use validation results from FarmCropViewModel */ try { var farmCrop = mapper.Map <FarmCropDto>(model); farmService.AddFarmCrop(farmCrop); } catch (ValidationException ex) { ModelState.AddModelError(ex.Property, ex.Message); return(View(model)); } return(RedirectToAction("List")); }