public ActionResult Edit(int?id, CustomerEditContactInfo newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.CustomerId })); } if (id.GetValueOrDefault() != newItem.CustomerId) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } // Attempt to do the update var editedItem = m.CustomerEditContactInfo(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.CustomerId })); } else { // Show the details view, which will have the updated data return(RedirectToAction("details", new { id = newItem.CustomerId })); } }
// GET: Customers public ActionResult Index() { // Attention - Error - object reference not set to an instance of an object // Just as it says... an object is null, and you're trying to use it // Set a breakpoint on line 24 above, then "step over" to get to the error // Inspect the variable values as you step through the lines of code // Create a new CustomerEditContactInfo object // Intentionally leave out some property values var newCust = new CustomerEditContactInfo { //Fax = "(416) 661-4034", //Email = "*****@*****.**", CustomerId = 2, Phone = "(416) 491-5050" }; // Now, attempt to use some values that are not set var email = newCust.Email; var emailLength = email.Length; // Fetch the collection var c = m.CustomerGetAll(); // Pass the collection to the view return(View(c)); }
// GET: Customers public ActionResult Index() { // Attention - Error - object reference not set to an instance of an object // Just as it says... an object is null, and you're trying to use it // Set a breakpoint on line 24 above, then "step over" to get to the error // Inspect the variable values as you step through the lines of code // Create a new CustomerEditContactInfo object // Intentionally leave out some property values var newCust = new CustomerEditContactInfo { //Fax = "(416) 661-4034", //Email = "*****@*****.**", CustomerId = 2, Phone = "(416) 491-5050" }; // Now, attempt to use some values that are not set var email = newCust.Email; var emailLength = email.Length; // Fetch the collection var c = m.CustomerGetAll(); // Pass the collection to the view return View(c); }
// ProductEdit() public CustomerBase CustomerEditContactInfo(CustomerEditContactInfo newItem) { var o = ds.Customers.Find(newItem.CustomerId); if (o == null) { return(null); } else { ds.Entry(o).CurrentValues.SetValues(newItem); ds.SaveChanges(); return(mapper.Map <Customer, CustomerBase>(o)); } }
public ActionResult Edit(int?id, CustomerEditContactInfo newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.CustomerId })); } if (id.GetValueOrDefault() != newItem.CustomerId) { // This appears to be data tampering, so redirect the user away return(RedirectToAction("index")); } // Attention 03 (web app) - Error - cause a validation error when saving to the store // At this point in the method, newItem has passed the initial validation test // Set a breakpoint at line 139 below // So, let's invalidate some of its values (null or string too long), // and pass it to the manager, which will attempt to save it to the data store // Then, BOOM!, a DbEntity validation error will appear // Set a required value to null newItem.Email = null; // Set another to a too-long value newItem.Phone = "This string is too long. It exceeds the 24-character limit."; // Attempt to do the update var editedItem = m.CustomerEditContactInfo(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return(RedirectToAction("edit", new { id = newItem.CustomerId })); } else { // Show the details view, which will have the updated data return(RedirectToAction("details", new { id = newItem.CustomerId })); } }
public CustomerBase CustomerEditContactInfo(CustomerEditContactInfo newItem) { // Attempt to fetch the object var o = ds.Customers.Find(newItem.CustomerId); if (o == null) { // Problem - item was not found, so return return(null); } else { // Update the object with the incoming values ds.Entry(o).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return(mapper.Map <CustomerBase>(o)); } }
public CustomerBase CustomerEditContactInfo(CustomerEditContactInfo newItem) { // Attempt to fetch the object var o = ds.Customers.Find(newItem.CustomerId); if (o == null) { // Problem - item was not found, so return return null; } else { // Update the object with the incoming values ds.Entry(o).CurrentValues.SetValues(newItem); ds.SaveChanges(); // Prepare and return the object return Mapper.Map<CustomerBase>(o); } }
public ActionResult Edit(int?id, CustomerEditContactInfo newItem) { if (!ModelState.IsValid) { return(RedirectToAction("edit", new { id = newItem.CustomerId })); } if (id.GetValueOrDefault() != newItem.CustomerId) { return(RedirectToAction("index")); } var editedItem = m.CustomerEditContactInfo(newItem); if (editedItem == null) { return(RedirectToAction("edit", new { id = newItem.CustomerId })); } else { return(RedirectToAction("details", new { id = newItem.CustomerId })); } }
public ActionResult Edit(int? id, CustomerEditContactInfo newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.CustomerId }); } if (id.GetValueOrDefault() != newItem.CustomerId) { // This appears to be data tampering, so redirect the user away return RedirectToAction("index"); } // Attention - Error - cause a validation error when saving to the store // At this point in the method, newItem has passed the initial validation test // Set a breakpoint at line 139 below // So, let's invalidate some of its values (null or string too long), // and pass it to the manager, which will attempt to save it to the data store // Then, BOOM!, a DbEntity validation error will appear // Set a required value to null newItem.Email = null; // Set another to a too-long value newItem.Phone = "This string is too long. It exceeds the 24-character limit."; // Attempt to do the update var editedItem = m.CustomerEditContactInfo(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.CustomerId }); } else { // Show the details view, which will have the updated data return RedirectToAction("details", new { id = newItem.CustomerId }); } }
public ActionResult Edit(int? id, CustomerEditContactInfo newItem) { // Validate the input if (!ModelState.IsValid) { // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.CustomerId }); } if (id.GetValueOrDefault() != newItem.CustomerId) { // This appears to be data tampering, so redirect the user away return RedirectToAction("index"); } // Attempt to do the update var editedItem = m.CustomerEditContactInfo(newItem); if (editedItem == null) { // There was a problem updating the object // Our "version 1" approach is to display the "edit form" again return RedirectToAction("edit", new { id = newItem.CustomerId }); } else { // Show the details view, which will have the updated data return RedirectToAction("details", new { id = newItem.CustomerId }); } }