public ActionResult EditCustomer(Models.CustomerModels model) { if (ModelState.IsValid) { try { JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities(); var queryCustomer = (from p in _dbContext.T_Customer where p.vCustomerName == model.custumerName select p).FirstOrDefault(); model._StatesList = Helper.Helper.GetUSAStates(); //queryCustomer.custumerName = model.custumerName; queryCustomer.vCustomerAddress = model.customerAddress; queryCustomer.vStateCode = model.states; queryCustomer.vCustomerCity = model.customerCity; _dbContext.SaveChanges(); JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContextContacts = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities(); var queryContactsByCustomer = (from p in _dbContextContacts.T_ContactsByCustomer where p.vCustomerName == model.custumerName select p); foreach (var item in queryContactsByCustomer) { _dbContextContacts.T_ContactsByCustomer.Remove(item); } _dbContextContacts.SaveChanges(); foreach (var item in model._ListContacts) { JneCommSitesDataLayer.T_ContactsByCustomer newContactByCustomer = new JneCommSitesDataLayer.T_ContactsByCustomer(); newContactByCustomer.vContactName = item.contactName; newContactByCustomer.vEmailContact = item.contactEmail; newContactByCustomer.vPhoneContact = item.contactPhone; newContactByCustomer.vAreaContact = item.contactArea; newContactByCustomer.vCustomerName = model.custumerName; _dbContext.T_ContactsByCustomer.Add(newContactByCustomer); _dbContext.SaveChanges(); } return(Json(true)); } catch (Exception error) { return(Json(error.Message)); } } return(Json("There is a error, Please try again.")); }
public ActionResult CreateCustomer(Models.CustomerModels model) { if (ModelState.IsValid) { JneCommSitesDataLayer.JneCommSitesDataBaseEntities _dbContext = new JneCommSitesDataLayer.JneCommSitesDataBaseEntities(); var queryCustomer = (from p in _dbContext.T_Customer where p.vCustomerName == model.custumerName select p).FirstOrDefault(); if (queryCustomer != null) { ModelState.AddModelError(string.Empty, "The Customer already exist"); model._StatesList = Helper.Helper.GetUSAStates(); return(Json("The Customer already exist")); } JneCommSitesDataLayer.T_Customer newCustomer = new JneCommSitesDataLayer.T_Customer(); newCustomer.vCustomerName = model.custumerName; newCustomer.vCustomerAddress = model.customerAddress; newCustomer.vCustomerCity = model.customerCity; newCustomer.vStateCode = model.states; _dbContext.T_Customer.Add(newCustomer); _dbContext.SaveChanges(); foreach (var item in model._ListContacts) { JneCommSitesDataLayer.T_ContactsByCustomer newContactByCustomer = new JneCommSitesDataLayer.T_ContactsByCustomer(); newContactByCustomer.vContactName = item.contactName; newContactByCustomer.vEmailContact = item.contactEmail; newContactByCustomer.vPhoneContact = item.contactPhone; newContactByCustomer.vAreaContact = item.contactArea; newContactByCustomer.vCustomerName = model.custumerName; _dbContext.T_ContactsByCustomer.Add(newContactByCustomer); _dbContext.SaveChanges(); } return(Json(true)); } if (model._ListContacts == null) { return(Json("You need add a contact for the costumer.")); } return(Json("There is a error, please try again.")); }