Jestem początkujący w programowaniu.
Głowię się od dwóch dni w jaki sposób mogę wyświetlić wszystkie dane samochodu wraz ze zdjęciami o określonym id. Już mi powoli psycha siada. xd
Mam tabelę "Cars" oraz "CarImages" (tutaj są przechowywane zdjęcia samochodów)
Tak wygląda model Car
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace autoleasing.Models
{
[Table("Cars")]
public class CarModel
{
[Key]
public int Id { get; set; }
public string Brand { get; set; }
public string BodyType { get; set; }
public string GearboxType { get; set; }
public string ProductionYear { get; set; }
public string Mileage { get; set; }
public string EnginePower { get; set; }
public string EngineCapacity { get; set; }
public string FuelType { get; set; }
public int Count { get; set; }
public bool Available { get; set; }
public List<CarImage> AllImages { get; set; }
}
}
A tak wygląda model CarImage
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace autoleasing.Models
{
public class CarImage
{
[Key]
public int Id { get; set; }
public string ImageName { get; set; }
[ForeignKey("CarModel")]
public int CarId { get; set; }
public CarModel CarModel { get; set; }
}
}
Tak prezentuje się DetailsViewModel
using autoleasing.Models;
namespace autoleasing.ViewModels
{
public class DetailsViewModel
{
public int Id { get; set; }
public string Brand { get; set; }
public string BodyType { get; set; }
public string GearboxType { get; set; }
public string ProductionYear { get; set; }
public string Mileage { get; set; }
public string EnginePower { get; set; }
public string EngineCapacity { get; set; }
public string FuelType { get; set; }
public int Count { get; set; }
public bool Available { get; set; }
public List<CarImage> Images { get; set; }
}
}
W CarRepository w ten sposób pobieram dane samochodu o podanym Id
public CarImage GetById_test(int id)
{
return _context.CarImages.Include(p => p.CarModel).FirstOrDefault(x => x.CarId == id);
}
W ten sposób próbuję w kontrolerze zmapować dane z modelu do viewmodelu (zapewne jest to źle, ale no już pokażę moje wypociny)
[Route("Cars/Details/{id}")]
[HttpGet]
public ActionResult Details(int id)
{
var carImage = _carRepository.GetById_test(id);
if (carImage == null) return View("Error");
var list = new List<CarImage>();
foreach (var img in carImage.ImageName)
{
list.Add(new CarImage( { ImageName = img });
}
var carVM = new DetailsViewModel
{
Brand = carImage.CarModel.Brand,
BodyType = carImage.CarModel.BodyType,
GearboxType = carImage.CarModel.GearboxType,
ProductionYear = carImage.CarModel.ProductionYear,
Mileage = carImage.CarModel.Mileage,
EnginePower = carImage.CarModel.EnginePower,
EngineCapacity = carImage.CarModel.EngineCapacity,
FuelType = carImage.CarModel.FuelType,
Count = carImage.CarModel.Count,
Available = carImage.CarModel.Available,
Images = list
};
return View(carVM);
}
Jaki sposób mogę wyświetlić wszystkie dane samochodu wraz ze zdjęciami o określonym id?