我不确定有什么问题,在我的家庭控制器中我从数据库收集数据并将其分配给模型
控制器
public ActionResult Index()
{
//Edited For Security Reasons
UserManager.Manager manager = new UserManager.Manager();
var data = manager.GetAllUsers();
var userDetails = from u in data
select new UserDetail
{
UserID = u.Id,
Username = u.Name,
UserLogin = u.Login,
FirstName = u.FirstName,
Surname = u.Surname,
Email = u.Email,
Active = Convert.ToInt32(u.Active),
RightID = u.RightsId,
GroupID = u.GroupsId,
};
userDetails.ToList();
return View();
}
这填充了userDetails,因为它在return View();
模型
public class UserDetail
{
public int UserID { get; set; }
public string Username { get; set; }
public string UserLogin { get; set; }
public string FirstName { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
public int Active { get; set; }
public List<int> RightID { get; set; }
public List<int> GroupID { get; set; }
}
Visual Studio告诉我问题在于我的@foreach (var item in Model)
它正在返回null
,但我不明白它在家中填充时的null
是怎样的控制器。
查看
@model IEnumerable<WebApplication1.Models.UserDetail>
<table class="table" id="tbData" cellspacing="5" border="1">
<tr>
<th>UserID</th>
<th>UserLogin</th>
<th>FirstName</th>
<th>Surname</th>
<th>Email</th>
<th>Active/th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(model => item.UserID)</td>
<td>@Html.DisplayFor(model => item.UserLogin)</td>
<td>@Html.DisplayFor(model => item.FirstName)</td>
<td>@Html.DisplayFor(model => item.Surname)</td>
<td>@Html.DisplayFor(model => item.Email)</td>
<td>@Html.DisplayFor(model => item.Active)</td>
</tr>
}
</table>
感谢您的提前帮助
答案 0 :(得分:0)
你没有归还你的模特:
userDetails.ToList();
return View();
这样做:
return View(userDetails.ToList());
答案 1 :(得分:0)
您没有向您的观点返回任何内容,这就是您无法从数据库中获取任何数据的原因
所以你要做的就是将用户列表返回到视图中,并确保视图中的模型数据是 IEnumerable或List
var userDetails = from u in data
select new UserDetail
{
UserID = u.Id,
Username = u.Name,
UserLogin = u.Login,
FirstName = u.FirstName,
Surname = u.Surname,
Email = u.Email,
Active = Convert.ToInt32(u.Active),
RightID = u.RightsId,
GroupID = u.GroupsId,
}.ToList();
return View(userDetails)