public IActionResult Add(AddCatBindingModel model) { if (!this.TryValidateModel(model)) { ViewData["Message"] = "Error!"; return(View()); } Cat cat = new Cat() { Name = model.Name, Age = model.Age, Breed = model.Breed, ImageUrl = model.ImageUrl }; int id = 0; using (var db = new FDMCDbContext()) { db.Cats.Add(cat); db.SaveChanges(); id = cat.Id; } return(RedirectToAction("Details", new { id })); }
public IActionResult Add(AddCatBindingModel model) { if (string.IsNullOrWhiteSpace(model.Name) || string.IsNullOrWhiteSpace(model.Breed)) { return(RedirectToAction("Add", "Cats")); } int.TryParse(model.Age, out int age); Cat cat = new Cat() { Name = model.Name, Age = age, Breed = model.Breed, ImageURL = model.ImageURL }; using (this.Context) { this.Context.Cats.Add(cat); this.Context.SaveChanges(); } return(RedirectToRoute("default", new { controller = "Cats", action = "Details", id = cat.Id })); }
public IActionResult Add(AddCatBindingModel bindingModel) { var cat = new Cat() { Name = bindingModel.Name, Age = bindingModel.Age, Breed = bindingModel.Breed, ImageUrl = bindingModel.ImageUrl }; this.dbContext.Cats.Add(cat); this.dbContext.SaveChanges(); return(RedirectToAction("Index", "Home")); }
public IActionResult Add(AddCatBindingModel model) { var cat = new Cat() { Name = model.Name, Age = model.Age, Breed = model.Breed, ImageUrl = model.ImageUrl }; this.context.Add(cat); this.context.SaveChanges(); return(this.Redirect("/")); }
public IActionResult Create(AddCatBindingModel bindingModel) { var catToCreate = new Cat { Name = bindingModel.Name, Age = bindingModel.Age, Size = bindingModel.Size, Color = bindingModel.Color, PictureURL = "https://th.bing.com/th/id/R80677ad4549c7ab35bc3e3cca9f5fa4e?rik=nlG0uuKC%2fVgkDg&pid=ImgRaw", CreatedAt = DateTime.Now }; dbContext.Cats.Add(catToCreate); dbContext.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult CreateCat([FromBody] AddCatBindingModel bindingModel) { var CatToCreate = new Cat { Name = bindingModel.Name, Age = bindingModel.Age, Size = bindingModel.Size, Color = bindingModel.Color, PictureURL = "https://th.bing.com/th/id/R80677ad4549c7ab35bc3e3cca9f5fa4e?rik=nlG0uuKC%2fVgkDg&pid=ImgRaw", CreatedAt = DateTime.Now }; var createdCat = dbContext.Cats.Add(CatToCreate).Entity; dbContext.SaveChanges(); return(Ok(CatToCreate.GetViewModel())); }