.net 核心 5 DropDownListFor
.net core 5 DropDownListFor
我正在显示索引视图中的数据以从 viewBag 获取用户角色。它不是 运行 显示错误
使用以下代码
控制器代码
private readonly ministrydbcContext _context;
List<RolesTbl> allRoles = new List<RolesTbl>();
public UserInfoController(ministrydbcContext context)
{
_context = context;
allRoles = _context.RolesTbls.ToList();
ViewBag.UserRole = new SelectList(allRoles, "RoleId", "RoleName");
}
public async Task<IActionResult> Index()
{
return View(await _context.UserInfoTbls.ToListAsync());
}
查看代码
@model IEnumerable<CoreProj.Models.UserInfoTbl>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.LoginName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LoginPass)
</td>
<td>
@Html.DisplayFor(modelItem => item.Edate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Euser)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mdate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Muser)
</td>
<td>
@Html.DropDownListFor(modelItem => item.UserRoleId, new SelectList(@ViewBag.UserRole, "RoleId", "RoleName"))
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.UserId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.UserId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.UserId">Delete</a>
</td>
</tr>
我遇到错误
ArgumentNullException:值不能为空。 (参数'items')
行下方出现错误
@Html.DropDownListFor(modelItem => item.UserRoleId, new SelectList(@ViewBag.UserRole, "RoleId", "RoleName"))
您可以像这样更改您的控制器。
private readonly ministrydbcContext _context;
public UserInfoController(ministrydbcContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var allRoles = await _context.RolesTbls.ToListAsync();
ViewBag.UserRole = allRoles;
return View(await _context.UserInfoTbls.ToListAsync());
}
我正在显示索引视图中的数据以从 viewBag 获取用户角色。它不是 运行 显示错误
使用以下代码
控制器代码
private readonly ministrydbcContext _context;
List<RolesTbl> allRoles = new List<RolesTbl>();
public UserInfoController(ministrydbcContext context)
{
_context = context;
allRoles = _context.RolesTbls.ToList();
ViewBag.UserRole = new SelectList(allRoles, "RoleId", "RoleName");
}
public async Task<IActionResult> Index()
{
return View(await _context.UserInfoTbls.ToListAsync());
}
查看代码
@model IEnumerable<CoreProj.Models.UserInfoTbl>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.LoginName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LoginPass)
</td>
<td>
@Html.DisplayFor(modelItem => item.Edate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Euser)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mdate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Muser)
</td>
<td>
@Html.DropDownListFor(modelItem => item.UserRoleId, new SelectList(@ViewBag.UserRole, "RoleId", "RoleName"))
</td>
<td>
<a asp-action="Edit" asp-route-id="@item.UserId">Edit</a> |
<a asp-action="Details" asp-route-id="@item.UserId">Details</a> |
<a asp-action="Delete" asp-route-id="@item.UserId">Delete</a>
</td>
</tr>
我遇到错误
ArgumentNullException:值不能为空。 (参数'items')
行下方出现错误
@Html.DropDownListFor(modelItem => item.UserRoleId, new SelectList(@ViewBag.UserRole, "RoleId", "RoleName"))
您可以像这样更改您的控制器。
private readonly ministrydbcContext _context;
public UserInfoController(ministrydbcContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var allRoles = await _context.RolesTbls.ToListAsync();
ViewBag.UserRole = allRoles;
return View(await _context.UserInfoTbls.ToListAsync());
}