Last active
April 5, 2019 18:54
-
-
Save efeozyer/99640a744700f2d3ccd993a77631ceb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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