Skip to content

Instantly share code, notes, and snippets.

@efeozyer
Last active April 5, 2019 18:54
Show Gist options
  • Save efeozyer/99640a744700f2d3ccd993a77631ceb5 to your computer and use it in GitHub Desktop.
Save efeozyer/99640a744700f2d3ccd993a77631ceb5 to your computer and use it in GitHub Desktop.
public class ProductController
{
[HttPost]
public ActionResult Create([FromBody] Product model)
{
using(var dbContext = new ProductContext())
{
dbContext.Products.Add(model);
dbContext.SaveChanges();
}
return View();
}
[HttpDelete("{id}")]
public ActionResult Delete(int? productId)
{
if (!productId.HasValue) {
ViewBag["Error"] = "Product Id must be specified.";
return View();
}
using(var dbContext = new ProductContext())
{
var product = dbContext.Where(x => x.Id == productId.Value).SingleOrDefault();
if (product == null) {
ViewBag["Error"] = $"Product could not be found, product id {productId.Value}";
return View();
}
dbContext.Add(product);
dbContext.SaveChanges();
}
return View();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment