public ActionResult Edit(EditProfile_ViewModel models) { if (ModelState.IsValid) { int SessionUserID = WebSecurity.GetUserId(User.Identity.Name); //COMMIT CHANGES TO DB var query = (from x in db.Customers where x.CustomerID.Equals(SessionUserID) select x).Single(); query.CustomerName = models.CustomerName; query.TFN = models.TFN; query.Address = models.Address; query.City = models.City; query.State = models.State; query.PostCode = models.PostCode; query.Phone = models.Phone; db.SaveChanges(); return RedirectToAction("MyProfile", "Profile"); } return View(models); }
public ActionResult Edit() { //This retrieves the userID that is associated with the username in the tables int SessionUserID = WebSecurity.GetUserId(User.Identity.Name); //Here im, fetching the information from the db entity. So when the view eg Form is rendered. //The relevant textboxes have the information from the DB prefilled. Instead of empty textboxes var query = (from x in db.Customers where x.CustomerID.Equals(SessionUserID) select x).Single(); //<-- Single() is required to return only one object instead of a, list containing 1 object //creating viewmodel and assinging data from the EF var model = new EditProfile_ViewModel() { CustomerName = query.CustomerName, TFN = query.TFN, Address = query.Address, City = query.City, State = query.State, PostCode = query.PostCode, Phone = query.Phone, }; return View(model); }
public ActionResult GridUpdate(int Customerid) { //Above, id passed in represents the unique primary key for each row, more properties can be passed in if // needed eg (int id,string CustomerName) //Creating another viewmodel so it can be used to data validation //need to assign it the customer id passed from the view so the object can be passed to the repositry method to add to db //TO BE IMPLEMENTED LATER, AT the moment, doing nothing major var customerObject = new EditProfile_ViewModel { CustomerID = Convert.ToInt32(Customerid) }; //Perform model binding from the view to the newly created viewmodel then validate if passes data annotations //Reference: http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel%28v=vs.108%29.aspx if (TryUpdateModel(customerObject)) { NWBAEntities db = new NWBAEntities(); Debug.WriteLine("Update Validation Passed"); Customer customerRecord = (from x in db.Customers where x.CustomerID.Equals(Customerid) select x).SingleOrDefault(); customerRecord.CustomerName = customerObject.CustomerName; customerRecord.TFN = customerObject.TFN; customerRecord.Address = customerObject.Address; customerRecord.City = customerObject.City; customerRecord.State = customerObject.State; customerRecord.PostCode = customerObject.PostCode; customerRecord.Phone = customerObject.Phone; db.SaveChanges(); Debug.WriteLine("Updated Record in DB"); return RedirectToAction("ChangeUserDetails", this.GridRouteValues()); } Debug.WriteLine("Update Validation Failed"); return RedirectToAction("ChangeUserDetails", this.GridRouteValues()); }