我想将UserId
表格中的UserName
和AspNetUsers
列列入我的管理区EditProfile
视图。
我的EditProfile模型是:
namespace AdminPanelExercises1.Areas.admin.Models
{
public class EditProfileModel
{
public int ID { get; set; }
public string UserID { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
public bool IsAdmin { get; set; }
}
}
我的EditProfile控制器是:
private AdminPanelExercises1.Models.ApplicationUser db3 = new AdminPanelExercises1.Models.ApplicationUser();
// GET: /admin/EditProfile/
public ActionResult Index()
{
EditProfileModel EPM = new EditProfileModel();
EPM.UserID = db3.Id;
EPM.UserName = db3.UserName;
return View(EPM);
}
我的EditProfile视图是:
@model IEnumerable<AdminPanelExercises1.Areas.admin.Models.EditProfileModel>
@{
ViewBag.Title = "Account Management";
Layout = "~/Areas/admin/Views/Shared/_layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserID)
</th>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserID)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</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>
我也得到了错误:
传递到字典中的模型项类型为&#39; AdminPanelExercises1.Areas.admin.Models.EditProfileModel&#39;,但此字典需要类型为&#39; System.Collections.Generic.IEnumerable的模型项`1 [AdminPanelExercises1.Areas.admin.Models.EditProfileModel]&#39;
如何解决此问题? 我是否写了错误的代码来从AspNetUsers表中获取数据?
答案 0 :(得分:2)
您的视图需要IEnumerable
(IEnumerable<AdminPanelExercises1.Areas.admin.Models.EditProfileModel>
)的模型,并且您从控制器返回单个项目。
您的视图应该有如下模型:
@model AdminPanelExercises1.Areas.admin.Models.EditProfileModel
如果您要在View
中显示多条记录,那么您的控制器应该返回多个项目(不是单个项目,如当前代码)