// This is the Insert method to insert the entered Contact item // USAGE: <asp:FormView InsertMethod="InsertItem"> public void InsertItem() { using (_db) { var item = new FrontierAg.Models.Contact(); item.DateCreated = DateTime.Now; TryUpdateModel(item); if (ModelState.IsValid) { // Save changes _db.Contacts.Add(item); if (item.Type == CType.Customer) { var item2 = new FrontierAg.Models.Shipping(); item2.Company = item.Company; item2.FName = item.FName; item2.LName = item.LName; item2.Other1 = ""; item2.Other2 = ""; item2.Address1 = item.Address1; item2.Address2 = item.Address2; item2.City = item.City; item2.State = item.State; item2.PostalCode = item.PostalCode; item2.Country = item.Country; item2.PPhone = item.PPhone; item2.SType = SType.Shipping; item2.DateCreated = item.DateCreated; item2.ContactId = item.ContactId; _db.Shippings.Add(item2); var item3 = new FrontierAg.Models.Shipping(); item3.Company = item.Company; item3.FName = item.FName; item3.LName = item.LName; item3.Other1 = ""; item3.Other2 = ""; item3.Address1 = item.Address1; item3.Address2 = item.Address2; item3.City = item.City; item3.State = item.State; item3.PostalCode = item.PostalCode; item3.Country = item.Country; item3.PPhone = item.PPhone; item3.SType = SType.Billing; item3.DateCreated = item.DateCreated; item3.ContactId = item.ContactId; _db.Shippings.Add(item3); } _db.SaveChanges(); Response.Redirect("~/Admin/Contacts/Default"); } } }
public void addShippingForm_InsertItem() { var item = new FrontierAg.Models.Shipping(); TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here using (FrontierAg.Models.ProductContext db = new FrontierAg.Models.ProductContext()) { db.Shippings.Add(item); db.SaveChanges(); } } }
// The id parameter name should match the DataKeyNames value set on the control public void ShippingsGrid_UpdateItem(int ShippingId) { FrontierAg.Models.Shipping item = null; item = _db.Shippings.Find(ShippingId); // Load the item here, e.g. item = MyDataLayer.Find(id); if (item == null) { // The item wasn't found ModelState.AddModelError("", String.Format("Item with id {0} was not found", ShippingId)); return; } TryUpdateModel(item); if (ModelState.IsValid) { // Save changes here, e.g. MyDataLayer.SaveChanges(); _db.SaveChanges(); } }