osyris zosar

osyris zosar

  • 1.8k
  • 215
  • 4.7k

Try to pass model in ViewModel into the Detail action

Apr 3 2021 6:26 AM
If have created a Viewmodel:

  1. public async Task<IActionResult> Details(Guid? id)  
  2.     {  
  3.   
  4.         if (id == null)  
  5.         {  
  6.             return NotFound();  
  7.         }  
  8.         ProductDetailViewModel productview = new ProductDetailViewModel()  
  9.         {  
  10.            productImages = await _context.ProductImages.FirstOrDefaultAsync( c => c.ProductId == id),  //error
  11.            productWithImagesDbs = await _context.ProductWithImagesDb.FirstOrDefaultAsync(m=> m.ProductId == id)  // error
  12.            
  13.         };  
  14.   
  15.         if (productview == null)  
  16.         {  
  17.             return NotFound();  
  18.         }  
  19.         return View(productview);  
  20.     }  
the viewmodel:
 
  1. public class ProductDetailViewModel  
  2. {  
  3.     public List<ProductWithImagesDb> productWithImagesDbs { getset; }  
  4.     public List<ProductImages> productImages { getset; }  
  5. }  
 the model: 
  1. public class ProductImages  
  2. {  
  3.     [Key]  
  4.     [Required]  
  5.     public Guid ProductId { getset; }  
  6.     [Required]  
  7.     public string ImagePath { getset; }  
  8.     [Required]  
  9.     public int ImageOrder { getset; }  
  10. }  
  1. public class ProductWithImages    
  2.     {    
  3.         [Key]    
  4.         [Required]    
  5.         public Guid ProductId { getset; }    
  6.         [Required]    
  7.         [Display(Name ="Product Title")]    
  8.         public string ProductTitle { getset; }    
  9. // etc...    
 
 Its probaly something really simple that i dont know about yet. but i can seem to figure it out 
it gives the error : Cannot implicitly convert type Test4.Models.ProductImagesDb to System.Collections.Generics.List<Test4.models.ProductImagesDb>
 
 
 
 

Answers (6)