How can we pass following ViewModel
- that is loaded via LINQ
query's Select Statement - to a View. I'm getting the error shown below. Please note that I still want to load ViewModel via select statement of LINQ query:
Model:
Public class Customer
{
Public int CustomerId {get; set;}
Public string FullName {get; set;}
...
}
Public class Order
{
Public int OrderId {get; set;}
Public int CustomerId {get; set;}
Public float Price {get; set;}
...
}
ViewModel:
Public class CustomViewModel
{
Public String Name {get; set;}
Public float Price {get; set;}
...
}
View:
@model myProject.Models.CustomViewModel
...
Controller:
var qry = from c in _context.Customers
join o in context.Orders on c.CustomerId = o.CustomerId
select new CustomViewModel { Name = c.FullName, Price = o.Price };
return View(qry);
Error:
The model item passed into the ViewDataDictionary is of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[myProject.Models.CustomViewModel]. But this ViewDataDictionary instance requires a model item of type 'myProject.Models.CustomViewModel]'