Skip to content
Loading
Cascading Dropdownlist - Registering new Store
 Ok, so, yes, this is not working because I'm using a Store model in the Index:
  1. @model List  
and at the same time I'm trying to insert a dropdownlist for the Department class which has no direct relation to the Store class. What alternative I have? I was thinking of using a ViewModel which would have both classes, like this:
  1.   public class StoreLocationViewModel  
  2.   {  
  3. public Store Stores { getset; }  
  4. public Department Departments { getset; }  
  5.   }  
 With this, I would have to change my Index model like this: 
  1. @model List  
 but I get this error:
 
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List`1[Application.Models.Store]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List`1[Application.Models.StoreLocationViewModel]'.
 
The error is now in the Controller, since I'm sending this: 
  1. var plataformaContext = _context.Stores.Include(c => c.StoreChains).Include(s => s.Districts)  
  2.                 .ThenInclude(d=>d.Departments);  
 and no a StoreLocationViewModel, BUT, I can't send a StoreLocationViewModel because then I would require to add the StoreLocationViewModel to the Database, define a Key and I believe that is wrong, right?
 
I'm very experienced on this, so I thank you for the time to read this. Any help is welcome as always!
 
Thanks,
 
Regards. 

1 Reply

Know the answer? Post it — somebody with the same question will find it here.