public ActionResult Create(CupcakeModel model) { //create cupcake if (ModelState.IsValid) { var cupcake = model.ToEntity(); _cupcakeService.InsertCupcake(cupcake); return(RedirectToAction("List")); } return(View(model)); }
public static CupcakeModel ToModel(this Cupcake cupcake) { //convert entity to model var model = new CupcakeModel(); model.Id = cupcake.Id; model.Name = cupcake.Name; model.Price = cupcake.Price; model.CreatedOn = (DateTime?)cupcake.CreatedOn; model.LastModified = (DateTime?)cupcake.LastModified; model.Ingredients = cupcake.Ingredients; model.NumberOfIngredients = cupcake.Ingredients.Split(new string[] { Environment.NewLine }, StringSplitOptions.None) .Select(x => !string.IsNullOrEmpty(x)) .ToList().Count; model.ImageFileName = cupcake.ImageFileName; model.Featured = cupcake.Featured; return(model); }
public ActionResult Edit(int id, CupcakeModel model) { var cupcake = _cupcakeService.GetCupcakeById(id); if (cupcake != null) { var cupcakeEntity = model.ToEntity(); //because imagefile is read only and only updated with file upload control so we pass entity file name instead of null cupcakeEntity.ImageFileName = cupcake.ImageFileName; if (ModelState.IsValid) { _cupcakeService.UpdateCupcake(cupcakeEntity); } else { return(View(model)); } } return(RedirectToAction("List")); }
public static Cupcake ToEntity(this CupcakeModel model) { //convert model to entity var cupcake = new Cupcake(); cupcake.Id = model.Id; cupcake.Name = model.Name; cupcake.Price = model.Price; cupcake.Ingredients = model.Ingredients; cupcake.LastModified = DateTime.Now; if (cupcake.Id == 0) { cupcake.CreatedOn = DateTime.Now; } if (!string.IsNullOrEmpty(model.ImageFileName)) { cupcake.ImageFileName = model.ImageFileName; } cupcake.Featured = model.Featured; return(cupcake); }
public ActionResult Create() { var model = new CupcakeModel(); return(View(model)); }