public ActionResult Create([Bind(Include = "Name, UnitPrice, Quantity, UserName")] Product product) { try { if (ModelState.IsValid && this.User.Identity.Name == product.UserName) { product.UserId = User.Identity.GetUserId(); db.Products.Add(product); db.SaveChanges(); ViewBag.Succes = "Product " + product.Name + " succeseful added"; return(View()); } } catch (DataException) { ModelState.AddModelError("", "Unable to save changes. Try again or see your administrator"); } return(View(product)); }
public JsonResult CreateProduct(Product product) { try { if (!ModelState.IsValid) { return(Json(new { Result = "ERROR", Message = "Form is not valid! " + "Please correct it and try again." })); } var addedProduct = db.Products.Add(product); db.SaveChanges(); return(Json(new { Result = "OK", Record = addedProduct })); } catch (Exception ex) { return(Json(new { Result = "ERROR", Message = ex.Message })); } }