Asp.net Mvc 和核心,它是如何工作的 SelectListItem
Asp.net Mvc and Core , How it works SelectListItem
我在我的编辑操作中遇到用户编辑问题。当涉及到 departmentDropdownlist 时
当我单击一个用户进行编辑时,假设 he/she 属于管理,然后我想显示
在同一个下拉列表中的所有部门下方的下拉列表和offcourse中进行管理。
但是现在,当我单击“编辑”时,它会按 Id 顺序显示所有部门……就像我想添加一个新用户一样。
我有角色下拉列表。你很好。如果用户是 Employee .. 它首先显示 Employee 然后是角色列表的其余部分 .. 如果用户是 Admin ,那么它会显示 Admin
然后在该角色列表的其余部分下方。但是当涉及到部门时,它会显示第一个 DeptId = 1 的部门名称,依此类推。我在 Asp.Net Mvc 和 Core 中都试过了。这是我的 EditUser 和 EditUserViewModel
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
EditUserViewModel model = new EditUserViewModel();
model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem
{
Text = r.Name,
Value = r.Id
}).ToList();
model.DepartmentNames = context.Departments.Select(s => new SelectListItem
{
Text = s.DeptName,
Value = s.DeptId.ToString()
}).ToList();
if (!String.IsNullOrEmpty(id))
{
ApplicationUser user = await userManager.FindByIdAsync(id);
if (user != null)
{
model.Name = user.Name;
model.Email = user.Email;
model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id;
model.DeptId = context.Departments.Single(r => r.DeptName == context.Sites.....??????); //How to do here
// ViewBag.DeptId = new SelectList(context.Departments, "DeptId", "DeptName", model.DeptId); // Even like this , shows the first id departmentname
}
}
return PartialView("_EditUser", model);
}
我的 EditUserViewModel
public class EditUserViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<SelectListItem> ApplicationRoles { get; set; }
public string ApplicationRoleId { get; set; }
public List<SelectListItem> DepartmentNames { get; set; }
public int DeptId { get; set; } // I have DeptId too in my AspNetUser table
}
既然User
table有DepartmentId
,我不知道你为什么不给它分配model.DeptId
所以不用
model.DeptId = context.Departments.Single(r => r.DeptName == context.Sites.....??????);
您可以只分配用户 table 的 DepartmentId
,即
model.DeptId = user.DepartmentId;
在 Razor 上,当您使用 TagHelper
构建下拉菜单时,它应该 select 基于 id 的正确下拉选项值。
<select class="form-control" asp-for="DeptId" asp-items="DepartmentNames"></select>
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
EditUserViewModel model = new EditUserViewModel();
model.ApplicationRoles = roleManager.Roles.Select(r => new SelectListItem
{
Text = r.Name,
Value = r.Id
}).ToList();
model.DepartmentNames = context.Departments.Select(s => new SelectListItem
{
Text = s.DeptName,
Value = s.DeptId.ToString()
}).ToList();
if (!String.IsNullOrEmpty(id))
{
ApplicationUser user = await userManager.FindByIdAsync(id);
if (user != null)
{
model.Name = user.Name;
model.Email = user.Email;
model.ApplicationRoleId = roleManager.Roles.Single(r => r.Name == userManager.GetRolesAsync(user).Result.Single()).Id;
model.DeptId = context.Departments.Single(r => r.DeptName == context.Sites.....??????); //How to do here
// ViewBag.DeptId = new SelectList(context.Departments, "DeptId", "DeptName", model.DeptId); // Even like this , shows the first id departmentname
}
}
return PartialView("_EditUser", model);
}
我的 EditUserViewModel
public class EditUserViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public List<SelectListItem> ApplicationRoles { get; set; }
public string ApplicationRoleId { get; set; }
public List<SelectListItem> DepartmentNames { get; set; }
public int DeptId { get; set; } // I have DeptId too in my AspNetUser table
}
既然User
table有DepartmentId
,我不知道你为什么不给它分配model.DeptId
所以不用
model.DeptId = context.Departments.Single(r => r.DeptName == context.Sites.....??????);
您可以只分配用户 table 的 DepartmentId
,即
model.DeptId = user.DepartmentId;
在 Razor 上,当您使用 TagHelper
构建下拉菜单时,它应该 select 基于 id 的正确下拉选项值。
<select class="form-control" asp-for="DeptId" asp-items="DepartmentNames"></select>