我使用asp.net core
,當我通過linq lambda query
查看我得到這個錯誤:asp.net核心通LINQ拉姆達查看
An unhandled exception occurred while processing the request.
InvalidOperationException: The model item passed into
the ViewDataDictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType7`1[System.Int64]]',
but this ViewDataDictionary instance
requires a model item of type 'System.Collections.Generic.IEnumerable`1[HRMS.Salaries]'.
這是我的查詢:
public async Task<IActionResult> Index()
{
var salary = (from salaries in _context.Salaries select new { salaries.Id });
return View(await salary.ToListAsync());
}
,並在查看我使用:
@model IEnumerable<HRMS.Salaries>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
</tr>
}
你選擇的ID,但你的觀點是expectiong工資本身 –
同意@AdilMammadov。嘗試'var salary = _context.Salaries;' – tmg
我同意@AdilMammadov - 展開:您目前只選擇一個'IEnumerable'(如果'Id'是一個'int')並將它傳遞給您的View。您將只需要從查詢中選擇「工資」,然後將它們傳遞給您的視圖;或者你需要改變你的視圖中的'@ model'爲'@model IEnumerable ',並顯示你的'foreach'循環只有'@item –