private async Task <ActionResult> GetBikeByID(int id) { try { BikeDetailView bikeView = new BikeDetailView(await _http.Get <Bike>(HttpContext, $"/api/bike/{id}")); bikeView.BikeTypes = new SelectList(await GetBikeTypes(), "ID", "TypeName"); if (!String.IsNullOrWhiteSpace(bikeView.ImageLocation)) { bikeView.ImageLocation = ApiUri + bikeView.ImageLocation; } if (Request.Path.Contains("Details")) { return(View("Details", bikeView)); } else { return(View("Edit", bikeView)); } } catch (Exception ex) { _log.LogError(ex, ex.Message); return(RedirectToAction("Index", new { alertMsg = "Error retrieving bike." })); } }
public async Task <ActionResult> Create() { BikeDetailView bikeView = new BikeDetailView(); bikeView.BikeTypes = new SelectList(await GetBikeTypes(), "ID", "TypeName"); return(View(bikeView)); }
public async Task <ActionResult> Edit(Bike bike, HttpPostedFileBase file) { if (ModelState.IsValid) { if (bike.ID == 0) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } try { var content = new MultipartFormDataContent(); content.Add(new StringContent(bike.ID.ToString()), "id"); content.Add(new StringContent(bike.Name), "name"); content.Add(new StringContent(bike.Brand), "brand"); content.Add(new StringContent(bike.Wheels.ToString()), "wheels"); content.Add(new StringContent(bike.FrameMaterial), "framematerial"); content.Add(new StringContent(bike.BikeType.ID.ToString()), "biketypeid"); content.Add(new StringContent(bike.Price.ToString()), "price"); if (file != null) { content.Add(new StreamContent(file.InputStream), "image", file.FileName); } var response = await _http.Put <BikeDTO>(HttpContext, "/api/bike", content); return(RedirectToAction("Index", new { alertMsg = String.Format("Bike {0} updated successfully.", bike.Name) })); } catch (Exception ex) { _log.LogError(ex, ex.Message); ViewBag.SaveResult = "An error occured saving the bike. Contact the support team."; BikeDetailView bikeView = new BikeDetailView(bike); bikeView.BikeTypes = new SelectList(await GetBikeTypes(), "ID", "TypeName"); return(View(bikeView)); } } else { BikeDetailView bikeView = new BikeDetailView(bike); bikeView.BikeTypes = new SelectList(await GetBikeTypes(), "ID", "TypeName"); return(View(bikeView)); } }