public JsonResult PostingTheData([FromBody] TripModelView data) { try { if (ModelState.IsValid) { var newTrip = Mapper.Map <Trip>(data); Response.StatusCode = (int)HttpStatusCode.Created; newTrip.UserName = User.Identity.Name; // adding the username //save the data(trip) to the database _logger.LogInformation("Attempting to save " + data.Name + " in the database"); _repository.Add(newTrip); if (_repository.SaveAll()) { _logger.LogInformation("Successfully add the trip " + newTrip.Name + " in to the database"); return(Json(Mapper.Map <TripModelView>(newTrip))); } } // in case if data is not valid then do the following Response.StatusCode = (int)HttpStatusCode.BadRequest; _logger.LogError("Error occur may be due to the model Invalidation, pleae check the log for full trace : " + ModelState); return(Json(new { Message = "Invalid data", ErrorProne = ModelState })); }catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.ExpectationFailed; _logger.LogError("Error occur in the post api/trips service may be mapping, for trace error view the detail: " + ex); return(Json(new { Message = "Error occur in the mapping", Error = ex })); } }
public async Task <IActionResult> UploadTrips() { // Declare dictionary to create pair values var formValues = new Dictionary <string, string>(); // Set data Form formValues.Add("File", "Trips"); HttpContent DictionaryItems = new FormUrlEncodedContent(formValues); MultipartFormDataContent form = new MultipartFormDataContent(); // Add parameters form.Add(DictionaryItems, "Parameters"); // Get files if (Request.Form.Files.Count > 0) { foreach (var f in Request.Form.Files) { // Add the file content to the form form.Add(new StreamContent(f.OpenReadStream()) { Headers = { ContentLength = f.Length, ContentType = new MediaTypeHeaderValue(f.ContentType) } }, "File", f.FileName); } } // Create the htpp client var client = new HttpClient(); var response = await client.PostAsync("http://localhost:52131/" + "UploadTrips", form); // Holder result var tripModelView = new TripModelView(); // Verify the request was exceuted successfully if (response.IsSuccessStatusCode) { // Read the result var result = response.Content.ReadAsStringAsync().Result; // Format the result tripModelView = JsonConvert.DeserializeObject <TripModelView>(result); return(View("Report", tripModelView)); } else { return(View("Error", new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier })); } }
public IActionResult AddTrip(TripModelView model) { ViewBag.Title = "Add Trip Destination and Date"; if (!ModelState.IsValid) { return(View(model)); } TempData["Trip"] = JsonConvert.SerializeObject(model); //model; ViewBag.Title = $"Add Info for The {model.Accommodation}"; return(View("AddTripInfo", new InfoModelView())); }