Here is the controller:

public ActionResult CategoryImage(int? id) { if (id == null) { return HttpNotFound(); } CategoryGallery gallery = mc.CategoriesGallery.Include(g => g.ImagesGallery) .FirstOrDefault(g => g.Id == id); if (gallery == null) { return HttpNotFound(); } return PartialView(gallery); } 

src="image.ImageUrl.ToString()" returns a lot of images, it works without ToString()

src="image.ImageUrl.FirstOrDefault()" returns an empty string ... FirstOrDefault(i => i.CategoryGalleryId == Model.Id) - this option is also empty ...

I tried various variations, but I haven’t got the desired result yet ... Is this possible with the current data model?

  • Something is not entirely clear your question. src = "image.ImageUrl.ToString ()" returns a lot of pictures - makes you wonder how it is? What do you have and where exactly does not work? - Denis Bubnov
  • Do you have a CategoriesGallery entity that has a lot of ImagesGallery , and you want to choose one category and one picture for it? - teran
  • cannot write Include(g => g.ImagesGallery.FirstOrDefault() ) ? - teran
  • Include (g => g.ImagesGallery.FirstOrDefault ()) I also tried to use this combination)) - Macro
  • teran you correctly understood me, you need to select one category and remove only one curtain from this category. - Macro

1 answer 1

  var gallery = mc.CategoriesGallery .Include(g => g.ImagesGallery) .FirstOrDefault(g => g.Id == id) .Select(a => new { CategoriesGallery = a, ImagesGallery= a.ImagesGallery.OrderByDescending(r => r.Id).FirstOrDefault() });