public ActionResult Edit(int id) { var model = context.Books.Find(id); CreateBookVewModel newModel = new CreateBookVewModel() { Title = model.Title, Content = model.Content, SelectedValue = model.CategoryId.ToString() }; GetCategoriesInDropDownList(newModel); return View(newModel); }
public ActionResult Edit(CreateBookVewModel model, int id) { if (ModelState.IsValid) { var modelForEdit = context.Books.Find(id); modelForEdit.Title = model.Title; modelForEdit.Content = model.Content; modelForEdit.CategoryId = int.Parse(model.SelectedValue); modelForEdit.UserId = ControllerContext.HttpContext.User.Identity.GetUserId(); context.SaveChanges(); return RedirectToAction("Index"); } return View(model); }
public ActionResult Create(CreateBookVewModel model) { if (ModelState.IsValid) { Book newBook = new Book() { Title = model.Title, Content = model.Content, CategoryId = int.Parse(model.SelectedValue), UserId = ControllerContext.HttpContext.User.Identity.GetUserId() }; context.Books.Add(newBook); context.SaveChanges(); return Redirect("Index"); } GetCategoriesInDropDownList(model); return View(model); }
private void GetCategoriesInDropDownList(CreateBookVewModel model) { model.BookCategories = context.Categories.ToList().Select(c => new SelectListItem { Text = c.CategoryName, Value = c.Id.ToString() }).ToList(); }