public IActionResult Create(string productId, CategoryType type, Sex sex) { var model = new CreateSizeViewModel { AllSizes = service.GetSizes(type, sex).Select(s => s.Map <Size, SizeListItemViewModel>()).ToList() }; return(View(model)); }
public IHttpActionResult Post(CreateSizeViewModel newModel) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _service.Create(_mapper.Map <Size>(newModel)); return(Ok()); }
public async Task <ActionResult> Create(CreateSizeViewModel model) { if (!ModelState.IsValid) { return(View(model)); } Size size = new Size { Name = model.Name }; if (model.Name.Contains("/")) { size.Id = int.Parse(model.Name.Replace("/", "")); } else { if (context.Sizes.Any()) { size.Id = context.Sizes.Max(s => s.Id) + 1; } else { size.Id = 1; } } context.Sizes.Add(size); try { await context.SaveChangesAsync(); } catch (Exception ex) { string errorMessage = ex.Message; while (ex != null) { errorMessage = ex.Message; ex = ex.InnerException; } ModelState.AddModelError("", errorMessage); return(View(model)); } return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(CreateSizeViewModel model, string submit, string productId, CategoryType type, Sex sex) { if (!ModelState.IsValid) { model.AllSizes = service.GetSizes(type, sex).Select(s => s.Map <Size, SizeListItemViewModel>()).ToList(); return(View(model)); } if (model.SizeId == Guid.Empty) { ModelState.AddModelError("SizeId", "Please choose size"); model.AllSizes = service.GetSizes(type, sex).Select(s => s.Map <Size, SizeListItemViewModel>()).ToList(); return(View(model)); } var productCheck = Guid.TryParse(productId, out Guid parsedProductId); if (!productCheck) { this.TempData["Error"] = "Product does not exists."; return(RedirectToAction(nameof(ProductsController.Index), "Products")); } var size = model.Map <CreateSizeViewModel, ProductSize>(); size.ProductId = parsedProductId; await this.service.Create(size); this.TempData["Success"] = "Size added."; if (!string.IsNullOrEmpty(submit)) { return(RedirectToAction(nameof(ProductsController.Index), "Products")); } return(RedirectToAction(nameof(Create), new { productId = parsedProductId, type, sex })); }
public async Task <ActionResult <OkObjectViewModel <SizeDetailsViewModel> > > Create(CreateSizeViewModel model) { if (!ModelState.IsValid) { return(new BadRequestObjectResult(new BadRequestViewModel <ModelStateError>(ModelState.GetErrors()))); } var result = await this.manager.CreateProductSizeAsync(model); if (!result.Succeeded) { AddManagerErrorsToModelState(result.Errors); return(new BadRequestObjectResult(new BadRequestViewModel <ModelStateError>(ModelState.GetErrors()))); } var vm = this.manager.GetQueryById(result.Model.Id) .Select(SizeDetailsViewModel.FromEntity) .FirstOrDefault(); base.Response.StatusCode = (int)HttpStatusCode.Created; return(new OkObjectViewModel <SizeDetailsViewModel>("Success", vm)); }