public IHttpActionResult PutCustomer(int id, Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.CustomerID) { return(BadRequest()); } db.Entry(customer).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
protected void saveBtn_Click(object sender, EventArgs e) { if (!IsValid) { return; } var rawId = Request["ID"]; CustomerManagementModel.Customer customer; if (rawId == null) { customer = new CustomerManagementModel.Customer(); context.AddToCustomers(customer); } else { customer = GetCustomerById(Guid.Parse(rawId)); } customer.FirstName = firstNameField.Text; customer.LastName = LastNameField.Text; customer.Address = addressField.Text; customer.CountryID = (from c in context.Countries where c.Name == countries.Text select c.ID) .FirstOrDefault(); context.SaveChanges(); Response.Redirect("~/CustomerByCountry.aspx"); }
protected void btnSave_Click(object sender, EventArgs e) { if (IsValid) { Customer customer = getCustomer(); customer.FirstName = tbFirstName.Text; customer.LastName = tbLastName.Text; customer.Address = tbAdress.Text; customer.CountryID = Guid.Parse(ddlCountry.SelectedValue); context.SaveChanges(); Response.Redirect("~/Customers.aspx"); } }
protected void btnUpdate_Click(object sender, EventArgs e) { try { using (CustomerManagementEntities context = new CustomerManagementEntities()) { foreach (XElement country in countriesWithPhNoFormat) { Guid g = new Guid(country.Attribute("ID").Value); Country c = context.Countries.Where(x => x.ID == g).FirstOrDefault <Country>(); c.PhoneNoFormat = country.Attribute("PhoneNoFormat").Value; } context.SaveChanges(); ImportResultLabel.Text = "Импорт завершен успешно"; } } catch (Exception ex) { ImportResultLabel.Text = ex.Message; } }
protected void updateBtn_Click(object sender, EventArgs e) { var doc = XElement.Load(Server.MapPath("~/App_Data/Countries.xml")); var formattedCountries = from c in doc.Elements("Country") where c.Attribute("PhoneNoFormat") != null && !string.IsNullOrWhiteSpace(c.Attribute("PhoneNoFormat").Value) select c; var countries = from formattedCountry in formattedCountries from c in context.Countries where c.ID.ToString() == formattedCountry.Attribute("ID").Value select c; Debug.WriteLine("withPhoneIds.Count() " + formattedCountries.Count()); Debug.WriteLine("countries.Count() " + countries.Count()); // db doesn't contain these countries, // so we'll add new records rather than updating foreach (var formattedCountry in formattedCountries) { var country = new Country { ID = Guid.Parse(formattedCountry.Attribute("ID").Value), Name = formattedCountry.Attribute("Name").Value, PhoneNoFormat = formattedCountry.Attribute("PhoneNoFormat").Value, DialingCountryCode = formattedCountry.Attribute("DialingCountryCode").Value, InternationalDialingCode = formattedCountry.Attribute("InternationalDialingCode").Value, InternetTLD = formattedCountry.Attribute("InternetTLD").Value }; context.AddToCountries(country); } context.SaveChanges(); Debug.WriteLine("withPhoneIds.Count() " + formattedCountries.Count()); Debug.WriteLine("countries.Count() " + countries.Count()); }