public async Task <JsonResult> Edit(TyreViewModel tyre) { try { var data = Db.Tyre.FirstOrDefault(t => t.Id == tyre.Id); if (data == null) { return(Json(new { ok = 400 }, JsonRequestBehavior.AllowGet)); } data.ModelId = tyre.ModelId; data.Width = tyre.Width; data.Height = tyre.Height; data.Radius = tyre.Radius; data.DisplayInDefault = tyre.DisplayInDefault; data.UsageStatus = tyre.UsageStatus; data.Price = tyre.Price; data.Stok = tyre.Stok; data.Season = tyre.Season; data.Status = tyre.Status; data.Description = tyre.Description; data.Sale = tyre.Sale; await Db.SaveChangesAsync(); var images = Db.Image.ToList(); if (tyre.ImageId != null) { foreach (var item in tyre.ImageId) { var image = images.FirstOrDefault(i => i.Id == int.Parse(item)); if (image != null) { image.TyreId = data.Id; } } await Db.SaveChangesAsync(); return(Json(new { ok = 200 }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { error = 400 }, JsonRequestBehavior.AllowGet)); } } catch (Exception) { Response.StatusCode = 400; throw; } }
public async Task <JsonResult> Create(TyreViewModel tyre) { try { var data = new Tyre { ModelId = tyre.ModelId, Width = tyre.Width, Height = tyre.Height, DisplayInDefault = tyre.DisplayInDefault, Radius = tyre.Radius, UsageStatus = tyre.UsageStatus, Price = tyre.Price, Stok = tyre.Stok, Season = tyre.Season, Status = tyre.Status, Description = tyre.Description, Sale = tyre.Sale }; Db.Tyre.Add(data); await Db.SaveChangesAsync(); var images = Db.Image.ToList(); if (tyre.ImageId != null) { foreach (var item in tyre.ImageId) { var image = images.FirstOrDefault(i => i.Id == int.Parse(item)); if (image != null) { image.TyreId = data.Id; } } await Db.SaveChangesAsync(); return(Json(new { ok = 200 }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { error = 400 }, JsonRequestBehavior.AllowGet)); } } catch (Exception) { Response.StatusCode = 400; throw; } }