public ActionResult Create(CreateMediaViewModel model) { try { string FileName = UploadFile(model.File) ?? string.Empty; if (ModelState.IsValid) { if (model.CategoryId == -1) { ModelState.AddModelError("", "Please review the input fields"); return(View(GetAllCategories())); } var Category = categoryRepository.Find(model.CategoryId); var media = new Media { Name = model.Name, Image = FileName, MediaCategory = Category }; repository.Add(media); return(RedirectToAction("index")); } return(View(model)); } catch { return(View(model)); } }
public ActionResult Create() { var model = new CreateMediaViewModel { Categories = FillCategory() }; return(View(model)); }
// if the client didn't select or select -1 value and submit the form then the form will return back // so we need to send the view model again with the categories CreateMediaViewModel GetAllCategories() { CreateMediaViewModel model = new CreateMediaViewModel { Categories = FillCategory() }; return(model); }