//public async Task<IActionResult> Edit(int id, [Bind("Id,CustomerId,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax")] Customers customers) public async Task <IActionResult> Edit(string customerId, [FromForm] CustomersForUpdate customer) { if (customerId != customer.CustomerId) { return(NotFound()); } if (ModelState.IsValid) { try { await _serviceCustomers.UpdateCustomer(customer); } catch (DbUpdateConcurrencyException) { if (await CustomersExists(customer.CustomerId) == false) { return(NotFound()); } else { throw; } } //return View(customer); return(RedirectToAction("Detail", new { customerId = customerId })); } return(View(customer)); }
public async Task <ActionResult> PutCustomer(int id, [FromBody] CustomersForUpdate customer) { _orm.OpenConn(); if (!await _orm.CustomerExist(id)) { return(NotFound()); } var customerFromDB = await _orm.GetCustomerById(id); //Map from customerFromDB (Source) to customer (Destination) //Apply Updated fields values to that dto //Map from customer (Source) to customerFromDB (Destination) //aka. copying values from source to destination _mapper.Map(customer, customerFromDB); if (await _orm.UpdateCustomer(customerFromDB) == 0) { return(BadRequest()); } await _orm.CloseConn(); return(NoContent()); }
public async Task UpdateCustomer(CustomersForUpdate customerToUpdate) { var serializedCustomerToUpdate = JsonConvert.SerializeObject(customerToUpdate); var request = new HttpRequestMessage(HttpMethod.Put, $"{apiRoute}/{customerToUpdate.CustomerId}"); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(mediaType)); request.Content = new StringContent(serializedCustomerToUpdate); request.Content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(mediaType); var response = await _httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); }