我收到以下錯誤消息。字典需要型號錯誤的型號項目
傳遞到詞典中的模型項的類型爲「System.Collections.Generic.List`1 [MYAPP.Models.User]」,但這 詞典需要類型 「System.Collections中的一個模型項.Generic.IEnumerable`1 [MYAPP.Models.ViewModel.AccountViewModel]」。
我的代碼如下:
視圖模型
public class AccountViewModel
{
...
public IQueryable<User> user { get; set; }
}
控制器
public class UserController : Controller
{
private REc db = new REc();
// GET: /User/
public ActionResult Index()
{
AccountViewModel avm = new AccountViewModel();
var users = db.Users.Include(u => u.USERROLE);
avm.user = users;
return View((avm.user).ToList());
}
VIEW
@model IEnumerable<SNAPAPP.Models.ViewModel.AccountViewModel>
@{
ViewBag.Title = "Index";
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.USERROLE)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.USERROLE)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>