public ActionResult Create(CreateSelectViewModel viewModel) { var validImageTypes = new string[] { "image/gif", "image/jpeg", "image/pjpeg", "image/png" }; var valids = viewModel.Options.Select(x => x.ValueFile != null && validImageTypes.Contains(x.ValueFile.ContentType)).Count(); if ((SelectType)Enum.Parse(typeof(SelectType), viewModel.Type, true) == SelectType.Image && valids != viewModel.Options.Count) { TempData["IsSuccess"] = false; TempData["Message"] = string.Format(@"Please choose either a GIF, JPG or PNG image"); } else if ((SelectType)Enum.Parse(typeof(SelectType), viewModel.Type, true) == SelectType.Image && valids == viewModel.Options.Count) { foreach (var option in viewModel.Options) { if (option.ValueFile != null) { using (System.Drawing.Image image = System.Drawing.Image.FromStream(option.ValueFile.InputStream, true, true)) { if (!Directory.Exists(Server.MapPath(PathConstant.SelectPath))) { Directory.CreateDirectory(Server.MapPath(PathConstant.SelectPath)); } var imagePath = Path.Combine(Server.MapPath(PathConstant.SelectPath), option.ValueFile.FileName); option.ValueFile.SaveAs(imagePath); option.Value = option.ValueFile.FileName; } } } } var request = viewModel.MapTo <CreateSelectRequest>(); var response = _selectService.Create(request); if (response.IsSuccess) { return(RedirectToAction("Index")); } return(View(viewModel)); }
public ActionResult Create() { var viewModel = new CreateSelectViewModel(); foreach (var name in Enum.GetNames(typeof(SelectType))) { viewModel.Types.Add(new SelectListItem { Text = name, Value = name }); } viewModel.Parents = _selectService.GetSelects(new GetSelectsRequest()) .Selects.Select(x => new SelectListItem { Value = x.Id.ToString(), Text = x.Name.ToString() }).ToList(); viewModel.Parents.Insert(0, new SelectListItem { Value = "0", Text = "No Parent" }); viewModel.IsActive = true; return(View(viewModel)); }