我正在尝试将两个 UserManager
Feedback
模型与一个 View 绑定(bind),但我遇到了问题,因为 View 不包含电子邮件的任何定义...此处是我的所有代码
public class FeedbackMixModel
{
public List<UserManager> allUserManagers { get; set; }
public List<Feedback> allfeedbacks { get; set; }
}
Controller ==>>>
var user = db.UserManagers.ToList();
var feedbacks = db.Feedbacks.ToList();
var Model = new FeedbackMixModel
{
allUserManagers =user,
allfeedbacks=feedbacks
};
return View(Model);
查看==>>
@model WebApplication5.Models.FeedbackMixModel
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<!-- Main content -->
<table class="pretty" id="dt">
<thead>
<tr>
<th >
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.CurrentStorage)
</th>
<th>
@Html.DisplayNameFor(model => model.MaxStorage)
</th>
<th>
@Html.DisplayNameFor(model => model.LastLoginMonth)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.allUserManagers)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.CurrentStorage)
</td>
<td>
@Html.DisplayFor(modelItem => item.MaxStorage)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastLoginMonth)
</td>
</tr>
}
</tbody>
</table>
我完全困惑如何解决这个问题...其中电子邮件在UserManager
类中定义,如果我做错了什么,我如何访问
最佳答案
您的实际 FeedbackMixModel
模型本身没有您打算用于 header 的属性的定义:
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.CurrentStorage)
</th>
<th>
@Html.DisplayNameFor(model => model.MaxStorage)
</th>
<th>
@Html.DisplayNameFor(model => model.LastLoginMonth)
</th>
</tr>
</thead>
您现有的模型仅包含两个属性:allUserManagers
和allFeedbacks
,因此它不知道什么Email
或其他一些属性你引用的是。根据您当前的代码,这些似乎是您的 UserManager
对象的实际属性,而不是您实际的 FeedbackMixModel
类。
您可以考虑对 header 进行硬编码以表达这些属性:
<thead>
<tr>
<th>
Email
</th>
<th>
Current Storage
</th>
<th>
Max Storage
</th>
<th>
Last Login Month
</th>
</tr>
</thead>
或者您可以尝试定位集合中的第一个元素,类似于 this approach :
<thead>
<tr>
<th>
@Html.DisplayNameFor(x => Model.allUserManagers[0].Email)
</th>
<th>
@Html.DisplayNameFor(x => Model.allUserManagers[0].CurrentStorage)
</th>
<th>
@Html.DisplayNameFor(x => Model.allUserManagers[0].MaxStorage)
</th>
<th>
@Html.DisplayNameFor(x => Model.allUserManagers[0].LastLoginMonth)
</th>
</tr>
</thead>
关于c# - ViewModel 不包含电子邮件的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38106398/