// Thêm một đối tượng public bool Insert(Phone model) { Phone dbEntry = context.Phones.Find(model.Id); if (dbEntry != null) { return(false); } context.Phones.Add(model); context.SaveChanges(); return(true); }
public ActionResult Create(Phone phone, HttpPostedFileBase fileupload) { string filename = ""; // Lấy id lớn nhất rồi công thêm 1 int lastId = int.Parse(db.Phones.ToList().OrderBy(e => int.Parse(e.Id.Trim())).Last().Id.Trim()) + 1; phone.Id = lastId.ToString(); if (fileupload != null) { filename = Path.GetFileName(fileupload.FileName); var path = Path.Combine(Server.MapPath("~/imageTel"), filename); if (System.IO.File.Exists(path)) { filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + filename; path = Path.Combine(Server.MapPath("~/imageTel"), filename); } fileupload.SaveAs(path); } phone.Image = filename; db.Phones.Add(phone); db.SaveChanges(); return(RedirectToAction("Index", "Phones")); }