public async Task<ActionResult> Create(InstrumentAdd newItem) { // Attention - Add a new item // The form accepts data and an optional image upload // Validate the input if (!ModelState.IsValid) { return View(); } // Process the input var addedItem = await m.AddInstrument(newItem); if (addedItem == null) { return View(newItem); } else { return RedirectToAction("details", new { id = addedItem.Item.Id }); } }
// Add a new instrument public async Task<InstrumentLinked> AddInstrument(InstrumentAdd newItem) { using (var request = CreateRequest()) { // Attempt to add the new item var response = await request.PostAsJsonAsync("instruments", newItem); if (response.IsSuccessStatusCode) { var addedItem = await response.Content.ReadAsAsync<InstrumentLinked>(); // Attempt to add the photo if (newItem.PhotoUpload != null) { await SetPhoto(addedItem.Item.Id, newItem.PhotoUpload); } return addedItem; } else { return null; } } }