我在Accouncontroller中有一个控制器,我得到了我想要的所有值。问题是我的viewmodel和view doesent似乎是对的,我得到:“传入字典的模型项是'System.Collections.Generic类型。列表1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [HomesProj.Models.UserRoleViewModel]”。
我的控制器看起来像这样:
public ActionResult Index()
{
var user = db.Users.ToList();
ApplicationDbContext context = new ApplicationDbContext();
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
List<String> list = new List<String>();
list.Add(user.ToString());
list.Add(roleManager.ToString());
return View(list);
}
我的Viewmodel看起来像这样:
public class UserRoleViewModel
{
public ICollection<ApplicationUser> User { get; set; }
public RoleManager<IdentityRole> Roles { get; set; }
}
我的观点是这样的:
@model IEnumerable<HomesProj.Models.UserRoleViewModel>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayName("Users")
</th>
<th>
@Html.DisplayName("Roles")
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(model => item.User)
</td>
<td>
@Html.DisplayFor(model => item.User)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
答案 0 :(得分:0)
错误消息是正确的
您传递了一个字符串列表:
List<String> list = new List<String>();
return View(list);
但接受枚举
@model IEnumerable<HomesProj.Models.UserRoleViewModel>
大概你打算传递一个UserRoleViewModel的列表?