public ActionResult Create(Tvset item) { using (var db = new TvContext()) { var comp = db.Companies.FirstOrDefault(x => x.Name == item.Company.Name); var tech = db.Technologies.FirstOrDefault(x => x.Id == item.TechnologyId); if (Request.Files.Count != 0) { item.ImageLink = GenerateImageLink(Request.Files[0]); } if (comp == null && !string.IsNullOrWhiteSpace(item.Company.Name)) { comp = new Company { Name = item.Company.Name }; db.Companies.Add(comp); db.SaveChanges(); } item.Company = comp; item.Technology = tech; if (ModelState.IsValid) { db.Tvsets.Add(item); db.SaveChanges(); return(RedirectToAction("View", new { id = item.Id })); } SelectList technologies = new SelectList(db.Technologies.ToList(), "Id", "Name"); ViewBag.Technologies = technologies; return(View()); } }
public ActionResult Edit(Tvset item) { using (var db = new TvContext()) { var comp = db.Companies.FirstOrDefault(x => x.Name == item.Company.Name); var tech = db.Technologies.FirstOrDefault(x => x.Id == item.TechnologyId); if (Request.Files.Count != 0) { item.ImageLink = GenerateImageLink(Request.Files[0]); } if (comp == null && !string.IsNullOrWhiteSpace(item.Company.Name)) { comp = new Company { Name = item.Company.Name }; db.Companies.Add(comp); db.SaveChanges(); } item.Company = comp; item.Technology = tech; var old = db.Tvsets.FirstOrDefault(x => x.Id == item.Id); if (ModelState.IsValid && old != null) { old.Name = item.Name; old.Resolution = item.Resolution; old.Size = item.Size; old.Technology = item.Technology; old.Year = item.Year; old.Company = item.Company; old.Details = item.Details; //удаление старой картинки if (!string.Equals(old.ImageLink, item.ImageLink) && item.ImageLink != null) { if (old.ImageLink != null) { var path = "~/img/" + Path.GetFileName(old.ImageLink); System.IO.File.Delete(HostingEnvironment.MapPath(path)); } old.ImageLink = item.ImageLink; } db.SaveChanges(); return(RedirectToAction("View", new { id = item.Id })); } SelectList technologies = new SelectList(db.Technologies.ToList(), "Id", "Name"); ViewBag.Technologies = technologies; return(View()); } }
public T Save(T entity) { if (Int32.Parse((entity).GetType().GetProperty("Id").GetValue(entity).ToString()) > 0) { Context.Set <T>().Attach(entity); Context.Entry(entity).State = EntityState.Modified; } else { Context.Set <T>().Add(entity); } Context.SaveChanges(); return(entity); }
private void button1_Click(object sender, EventArgs e) { TextBoxPopup tbp = new TextBoxPopup("Adjon meg egy szobaszámot:"); if (tbp.ShowDialog() == DialogResult.OK) { try { int roomNum = int.Parse(tbp.Input); using (TvContext context = new TvContext()) { if (context.Rooms.Find(roomNum) == null) { context.Rooms.Add(new Room() { RoomId = roomNum }); context.SaveChanges(); UpdateRoomsList(); } else { MessageBox.Show("A szoba már létezik.", "Létező szoba", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } catch (FormatException) { MessageBox.Show("A beírt érték nem szám. A szobaszám csak szám lehet.", "Hibás érték", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
public IActionResult Follow(int id, string title) { // fråga DB, finns detta IMDB-id redan? var tvTable = _tvContext.TvTable .SingleOrDefault(s => s.TvMazeId == id); // om showen inte fanns, nya upp den och spara i DB if (tvTable == null) { // nya upp en entitet tvTable = new TvTable { TvMazeId = id, Title = title }; // lägg till den nya entiteten till DB _tvContext.TvTable.Add(tvTable); _tvContext.SaveChanges(); // måste spara här för att få ett ID } // kolla att användaren inte redan följer serien int userId = GetUserId(); var tmp2 = _tvContext.UserToTvTable .SingleOrDefault(u => u.TvTableId == tvTable.Id && u.UserId == userId); // om användaren ej redan följer serien if (tmp2 == null) { // skapa ny post i UserToTvTable DB UserToTvTable userToTvTable = new UserToTvTable { TvTableId = tvTable.Id, UserId = userId, }; _tvContext.UserToTvTable.Add(userToTvTable); _tvContext.SaveChanges(); } return(RedirectToAction(nameof(UserPage))); }
public ActionResult Delete(int id) { using (var db = new TvContext()) { var item = db.Tvsets.Find(id); if (item != null) { //удаление картинки if (!string.IsNullOrWhiteSpace(item.ImageLink)) { var path = "~/img/" + Path.GetFileName(item.ImageLink); System.IO.File.Delete(HostingEnvironment.MapPath(path)); } db.Tvsets.Remove(item); db.SaveChanges(); } return(RedirectToAction("Index")); } }