entity framework - asp.net core pass linq lambda to view -


i'm using asp.net core, when pass linq lambda query view error:

an unhandled exception occurred while processing request.  invalidoperationexception: model item passed  viewdatadictionary of type 'system.collections.generic.list`1[<>f__anonymoustype7`1[system.int64]]', viewdatadictionary instance  requires model item of type 'system.collections.generic.ienumerable`1[hrms.salaries]'. 

this query:

public async task<iactionresult> index() {   var salary = (from salaries in _context.salaries select new { salaries.id });   return view(await salary.tolistasync()); } 

and in view use:

@model ienumerable<hrms.salaries>    @foreach (var item in model)   {     <tr>        <td>@item.id</td>      </tr>   }  

is there reason create object in query? if no, try instead :

public async task<iactionresult> index()         {             var salary = (from salaries in _context.salaries                           select salaries.id                       );             return view(await salary.tolistasync());         } 

then in view :

@model ienumerable<int>                      @foreach (var item in model)                     {                         <tr>                             <td>@item</td>                          </tr>                     }  

else, if need object, use in query :

public async task<iactionresult> index()             {                 var salary = (from salaries in _context.salaries                               select salaries                           );                 return view(await salary.tolistasync());             } 

and keep view same :

@model ienumerable<hrms.salaries>                          @foreach (var item in model)                         {                             <tr>                                 <td>@item.id</td>                              </tr>                         }  

edit : if want pass multiple fields view, it's better use new object. this, create class (for exemple, salarydetailsviewmodel) required fields. in controller :

public async task<iactionresult> index()         {             var salary = (from salaries in _context.salaries                           select new salarydetailsviewmodel {                                id = salaries.id,                               amount = salaries.amount,                               date = salaries.date,                               jobtitle = salaries.jobtitle.name }                       );             return view(await salary.tolistasync());         } 

then adjust view call different fields of custom object, display purpose, example :

@model ienumerable<salarydetailsviewmodel>                          @foreach (var item in model)                         {                             <tr>                                 <td>@item.id</td>                                 <td>@item.amount</td>                                 <td>@item.date</td>                                 <td>@item.jobtitle</td>                             </tr>                         }  

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -