1: [HttpPost]
2: [AjaxOnly]
3: [Authorize]
4: public JsonResult Edit(Product product)
5: {
6: if (this.ModelState.IsValid == true)
7: {
8: using (ProductContext ctx = new ProductContext())
9: {
10: Boolean success = false;
11:
12: ctx.Entry(product).State = (product.ProductId == 0) ? EntityState.Added : EntityState.Modified;
13:
14: try
15: {
16: success = (ctx.SaveChanges() == 1);
17: }
18: catch (DbUpdateConcurrencyException)
19: {
20: ctx.Entry(product).Reload();
21: }
22:
23: return (this.Json(new { Success = success, ProductId = product.ProductId, RowVersion = Convert.ToBase64String(product.RowVersion) }));
24: }
25: }
26: else
27: {
28: Dictionary<String, String> errors = new Dictionary<String, String>();
29:
30: foreach (KeyValuePair<String, ModelState> keyValue in this.ModelState)
31: {
32: String key = keyValue.Key;
33: ModelState modelState = keyValue.Value;
34:
35: foreach (ModelError error in modelState.Errors)
36: {
37: errors[key] = error.ErrorMessage;
38: }
39: }
40:
41: return (this.Json(new { Success = false, ProductId = 0, RowVersion = String.Empty, Errors = errors }));
42: }
43: }