[ad_1]
私はこの問題にほぼ 3 日を費やしました。 この段階を通過できないようで、そのまま放置されました。コードをツイクしてリウィークしたので、正しくやっていると思います。助けてください。 私は MVC API を使用していますが、問題は写真のアップロード、または部屋の詳細の投稿です。
できるだけ早くこの解決策が必要です。 私を助けてください
私が試したこと:
ルームモデル。
public class Room { public int Id { get; set; } [Required] [StringLength(20)] [Display(Name = "Room Number:")] public string RoomNumber { get; set; } [Display(Name = "Price:")] [DataType(DataType.Currency)] public decimal Rate { get; set; } [Display(Name = "Select Room Type:")] public int RoomTypeId { get; set; } public ICollection<RoomType> RoomType { get; set; } [Display(Name = "Select Booking Status:")] public int BookingStatusId { get; set; } public ICollection<BookingStatus> BookingStatus { get; set; } [StringLength(5000)] [DataType(DataType.MultilineText)] [Display(Name = "Description of Room:")] public string? Description { get; set; } public ICollection<Reservation> Reservations { get; set; } [NotMapped] public IFormFile CoverPhoto { get; set; } public string CoverPhotoURL { get; set; } [NotMapped] public IFormFileCollection GalleryFiles { get; set; } public List<RoomImage> Roomimages { get; set; } public bool isFavourite { get; set; } public bool IsAvailable { get; set; } = true; }
部屋のイメージモデル
public class RoomImage { public int Id { get; set; } public int RoomId { get; set; } public string Imageurl { get; set; } public Room Room { get; set; } //after migration uncomment the above }
部屋モデルを投稿するための MVC Web コントローラー
[HttpPost] public IActionResult AddNew([FromForm] Room model) { if (model != null) { // Access properties string roomnumber = model.RoomNumber; decimal rate = model.Rate; int roomtypeid = model.RoomTypeId; int statusid = model.BookingStatusId; string description = model.Description; bool isfavourite = model.isFavourite; bool isavailable = model.IsAvailable; // Create an instance of ImageData and set its properties var imageData = new Room { RoomNumber = roomnumber, Rate = rate, RoomTypeId = roomtypeid, BookingStatusId = statusid, Description = description, isFavourite = isfavourite, IsAvailable = isavailable, }; // Process the single file (IFormFile) if (model.CoverPhoto != null) { using (var memoryStream = new MemoryStream()) { model.CoverPhoto.CopyTo(memoryStream); byte[] imageBytes = memoryStream.ToArray(); // Process the single file data (imageBytes) // For example, you can convert it to Base64 if required: string base64String = Convert.ToBase64String(imageBytes); // Set the ImageBase64 property in the imageData imageData.CoverPhotoURL = model.CoverPhoto.FileName; } } // Process the multiple files (IFormFileCollection) if (model.GalleryFiles != null && model.GalleryFiles.Count > 0) { var fileDataList = new List<RoomImage>(); foreach (var file in model.GalleryFiles) { if (file != null && file.Length > 0) { using (var memoryStream = new MemoryStream()) { file.CopyTo(memoryStream); byte[] fileBytes = memoryStream.ToArray(); // Process each file data (fileBytes) // For example, you can save it, convert it to Base64, or perform other processing. // Access other information about the file string fileName = file.FileName; long fileSize = file.Length; // Create a FileData object to hold file information var fileData = new RoomImage { Imageurl = fileName }; // Add the file data to the list fileDataList.Add(fileData); } } } // Add the list of file data to the imageData imageData.Roomimages = fileDataList; } // Serialize the entire imageData to a JSON string string jsonModel = JsonConvert.SerializeObject(imageData); StringContent content = new StringContent(jsonModel, Encoding.UTF8, "application/json"); //httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", contxt.HttpContext.Session.GetString("accessToken")); HttpResponseMessage response = _httpclient.PostAsync(_httpclient.BaseAddress + "Rooms/addnewbook", content).Result; if (response.IsSuccessStatusCode) { TempData["suceessMessae"] = "New Room record created successfully."; return RedirectToAction("Index"); } return View(); } return BadRequest("No valid data was received."); }
これは Room Post リクエストの MVC API です。しかし、ここまでは到達しません。 そこにブレークポイントがあるため、APIコードに到達することはありません
[HttpPost("addnewbook")] public async Task<IActionResult> AddNewBookz([FromBody] Room room) { //if (ModelState.IsValid) // { if (room.CoverPhoto != null) { string folder = "rm_images/cover/"; room.CoverPhotoURL = await uploadimage(folder, room.CoverPhoto); } if (room.GalleryFiles != null) { string folder = "rm_images/rooms/"; room.Roomimages = new List<RoomImage>(); foreach (var file in room.GalleryFiles) { var gallery = new RoomImage() { Imageurl = await uploadimage(folder, file) }; room.Roomimages.Add(gallery); } } _context.Update(room); _context.SaveChanges(); // } return Ok(); }
バツ
[ad_2]
コメント