使用 asp.NET 核心标识设置 DropdownList 值
Setting DropdownList Value using asp.NET Core Identity
我有点乱,正在尝试清理。我有一个正确填充的 Company 下拉列表。但我无法弄清楚如何获得下拉列表的选定值。我看过很多例子,但在我们使用这个的上下文中,这让我感到困惑。
我们正在使用 asp.Net 核心身份。
感谢任何指导!
下拉列表:
<div class="form-group row text-center">
<label asp-for="CompanyLists" class="col-sm-3 text-right col-form-label labelFont"></label>
<div class="col-sm-8">
@Html.DropDownListFor(model => model.SelectedCompany, new SelectList(Model.CompanyLists.OrderBy(x => x.CompanyName), "CompanyID", "CompanyName"), "-- Select Company --", new { @class = "form-control" })
<span asp-validation-for="CompanyLists" class="text-danger"></span>
</div>
</div>
方法:
// EDIT USER : GET-----------------------------------------------------
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
//GET USER INFORMATION - EXIT IF USER DOESN'T EXIST
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
return View("NotFound");
}
//USER INFORMATION ---------------------------------------
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
//SelectedCompany = user.Company.ToString()
};
//COMPANY DROPDOWN INFO------------------------------------
var company = from c in companyRepository.GetCompanys() select c;
foreach (var c in company)
{
////Store this information into the company list in the viewmodel
var companyinfo = new EditUserViewModel.CompanyList
{
CompanyName = c.CompanyName,
CompanyID = c.CompanyId,
};
model.CompanyLists.Add(companyinfo);
};
//GET LIST OF ROLES(RoleID, RoleName)
var roles = roleManager.Roles;
foreach (var RoleName in roles)
{
//Execute identity method to get full information for the Role and store into an object (roleinfo)
var roleString = RoleName.Name;
var fullRoleInfo = await roleManager.FindByNameAsync(roleString);
//Store this information into the Role list in the viewmodel
var roleinfo = new EditUserViewModel.Role
{
RoleName = fullRoleInfo.Name,
RoleID = fullRoleInfo.Id,
};
if (await userManager.IsInRoleAsync(user, roleString))
{
roleinfo.IsSelected = true;
}
else
{
roleinfo.IsSelected = false;
}
model.Roles.Add(roleinfo);
};
//*****************************************************
//IDENTITY CLAIM INFORMATION ------------------------------
var existingUserClaims = await userManager.GetClaimsAsync(user);
foreach (Claim claim in ClaimStore.AllClaims)
{
var userClaims = new EditUserViewModel.Claim
{
ClaimType = claim.Type
};
if (existingUserClaims.Any(c => c.Type == claim.Type && c.Value == "true"))
{
userClaims.IsSelected = true;
}
else
{
userClaims.IsSelected = false;
}
model.Claims.Add(userClaims);
}
return PartialView("~/Views/Modals/_EditUserModalPartial.cshtml", model);
}
这是我开始感到困惑的地方。我们有一个EditUserViewModel.cs;但是,我们也有 ApplicationUser.cs 模型,我无法弄清楚所选公司如何变成 ApplicationUser.cs 或其他什么我需要做才能完成这项工作。
编辑用户视图模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace PortalDev.Models.ViewModels
{
public class EditUserViewModel
{
public EditUserViewModel()
{
Claims = new List<Claim>();
Roles = new List<Role>();
//CompanyLists = new List<ICompanyRepository>();
CompanyLists = new List<CompanyList>();
}
//ROLES ---------------------------------------------
public class Role
{
public string RoleName { get; set; }
public string RoleID { get; set; }
public bool IsSelected { get; set; }
}
public List<Role> Roles { get; set; }
//CLAIMS----------------------------------------------
public class Claim
{
public string ClaimType { get; set; }
public string ClaimID { get; set; }
public bool IsSelected { get; set; }
}
public List<Claim> Claims { get; set; }
//COMPANY DROPDOWN--------------------------------------
public class CompanyList
{
public string CompanyName { get; set; }
public int CompanyID { get; set; }
}
[Display(Name = "Company")]
public List<CompanyList> CompanyLists { get; set; } //List of Companies for dropdown
public string SelectedCompany { get; set; }
//USER INFORMATION --------------------------------------
public string Id { get; set; }
//[Required]
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public int CompanyId { get; set; }
}
}
ApplicationUser.cs
using Microsoft.AspNetCore.Identity;
using PortalDev.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//Extends built in IdentityUser class
namespace PortalDev.Models
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string Address{ get; set;}
public string City { get; set; }
public string State { get; set; }
public Company Company { get; set; }
}
}
UserMaint.html
@model IEnumerable<PortalDev.Models.ApplicationUser>
<link rel="stylesheet" href="~/css/Maintenance.css" />
<link rel="stylesheet" href="~/css/Index.css" />
<div class="tableContainer">
<div class="maintHeader">
<div class="headerText">
All Users
<button class="sqButton btnBlue float-right" title="Register" data-toggle="ajax-modal" data-target="#register" data-url="@Url.Action("Register", "Home")"><i class="glyphicon glyphicon-plus"></i></button>
</div>
</div>
@if (Model.Any())
{
//--Table Header Declaration--
<div class="wrapper">
<div class="scrollTable">
<table class="table table-hover table-md ">
<thead>
<tr>
<th class="text-left tableHead d-none">Id</th>
<th class="text-left tableHead">Company</th>
<th class="text-left tableHead">User Name</th>
<th class="text-left tableHead">First Name</th>
<th class="text-left tableHead">Last Name</th>
<th class="text-left tableHead">Title</th>
<th class="text-left tableHead">City</th>
<th class="text-left tableHead">State</th>
<th class="text-left tableHead text-right">Remove</th>
</tr>
</thead>
@*--Table Body For Each to pull DB records--*@
<tbody>
@foreach (var user in Model)
{
@Html.Partial("~/Views/Administration/Users/UserTable.cshtml", user)
}
</tbody>
</table>
</div>
</div>
}
<div id="modal-placeholder"></div>
</div>
如果要显示用户在 EditUser.cshtml
上编辑前的 Company
,可以对 ApplicationUser
模型、EditUser 方法和下拉列表进行以下修改如下所示:
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
public Company Company { get; set; }
}
EditUser 方法
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
CompanyId = user.CompanyId
};
公司下拉列表
<div class="col-sm-8">
@Html.DropDownListFor(model => model.CompanyId, new SelectList(Model.CompanyLists.OrderBy(x => x.CompanyName), "CompanyID", "CompanyName"), "-- Select Company --", new { @class = "form-control" })
<span asp-validation-for="CompanyId" class="text-danger"></span>
</div>
我有点乱,正在尝试清理。我有一个正确填充的 Company 下拉列表。但我无法弄清楚如何获得下拉列表的选定值。我看过很多例子,但在我们使用这个的上下文中,这让我感到困惑。 我们正在使用 asp.Net 核心身份。
感谢任何指导!
下拉列表:
<div class="form-group row text-center">
<label asp-for="CompanyLists" class="col-sm-3 text-right col-form-label labelFont"></label>
<div class="col-sm-8">
@Html.DropDownListFor(model => model.SelectedCompany, new SelectList(Model.CompanyLists.OrderBy(x => x.CompanyName), "CompanyID", "CompanyName"), "-- Select Company --", new { @class = "form-control" })
<span asp-validation-for="CompanyLists" class="text-danger"></span>
</div>
</div>
方法:
// EDIT USER : GET-----------------------------------------------------
[HttpGet]
public async Task<IActionResult> EditUser(string id)
{
//GET USER INFORMATION - EXIT IF USER DOESN'T EXIST
var user = await userManager.FindByIdAsync(id);
if (user == null)
{
ViewBag.ErrorMessage = $"User with Id = {id} cannot be found";
return View("NotFound");
}
//USER INFORMATION ---------------------------------------
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
//SelectedCompany = user.Company.ToString()
};
//COMPANY DROPDOWN INFO------------------------------------
var company = from c in companyRepository.GetCompanys() select c;
foreach (var c in company)
{
////Store this information into the company list in the viewmodel
var companyinfo = new EditUserViewModel.CompanyList
{
CompanyName = c.CompanyName,
CompanyID = c.CompanyId,
};
model.CompanyLists.Add(companyinfo);
};
//GET LIST OF ROLES(RoleID, RoleName)
var roles = roleManager.Roles;
foreach (var RoleName in roles)
{
//Execute identity method to get full information for the Role and store into an object (roleinfo)
var roleString = RoleName.Name;
var fullRoleInfo = await roleManager.FindByNameAsync(roleString);
//Store this information into the Role list in the viewmodel
var roleinfo = new EditUserViewModel.Role
{
RoleName = fullRoleInfo.Name,
RoleID = fullRoleInfo.Id,
};
if (await userManager.IsInRoleAsync(user, roleString))
{
roleinfo.IsSelected = true;
}
else
{
roleinfo.IsSelected = false;
}
model.Roles.Add(roleinfo);
};
//*****************************************************
//IDENTITY CLAIM INFORMATION ------------------------------
var existingUserClaims = await userManager.GetClaimsAsync(user);
foreach (Claim claim in ClaimStore.AllClaims)
{
var userClaims = new EditUserViewModel.Claim
{
ClaimType = claim.Type
};
if (existingUserClaims.Any(c => c.Type == claim.Type && c.Value == "true"))
{
userClaims.IsSelected = true;
}
else
{
userClaims.IsSelected = false;
}
model.Claims.Add(userClaims);
}
return PartialView("~/Views/Modals/_EditUserModalPartial.cshtml", model);
}
这是我开始感到困惑的地方。我们有一个EditUserViewModel.cs;但是,我们也有 ApplicationUser.cs 模型,我无法弄清楚所选公司如何变成 ApplicationUser.cs 或其他什么我需要做才能完成这项工作。
编辑用户视图模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace PortalDev.Models.ViewModels
{
public class EditUserViewModel
{
public EditUserViewModel()
{
Claims = new List<Claim>();
Roles = new List<Role>();
//CompanyLists = new List<ICompanyRepository>();
CompanyLists = new List<CompanyList>();
}
//ROLES ---------------------------------------------
public class Role
{
public string RoleName { get; set; }
public string RoleID { get; set; }
public bool IsSelected { get; set; }
}
public List<Role> Roles { get; set; }
//CLAIMS----------------------------------------------
public class Claim
{
public string ClaimType { get; set; }
public string ClaimID { get; set; }
public bool IsSelected { get; set; }
}
public List<Claim> Claims { get; set; }
//COMPANY DROPDOWN--------------------------------------
public class CompanyList
{
public string CompanyName { get; set; }
public int CompanyID { get; set; }
}
[Display(Name = "Company")]
public List<CompanyList> CompanyLists { get; set; } //List of Companies for dropdown
public string SelectedCompany { get; set; }
//USER INFORMATION --------------------------------------
public string Id { get; set; }
//[Required]
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
[Required]
[EmailAddress]
public string Email { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public int CompanyId { get; set; }
}
}
ApplicationUser.cs
using Microsoft.AspNetCore.Identity;
using PortalDev.Models.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
//Extends built in IdentityUser class
namespace PortalDev.Models
{
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string Address{ get; set;}
public string City { get; set; }
public string State { get; set; }
public Company Company { get; set; }
}
}
UserMaint.html
@model IEnumerable<PortalDev.Models.ApplicationUser>
<link rel="stylesheet" href="~/css/Maintenance.css" />
<link rel="stylesheet" href="~/css/Index.css" />
<div class="tableContainer">
<div class="maintHeader">
<div class="headerText">
All Users
<button class="sqButton btnBlue float-right" title="Register" data-toggle="ajax-modal" data-target="#register" data-url="@Url.Action("Register", "Home")"><i class="glyphicon glyphicon-plus"></i></button>
</div>
</div>
@if (Model.Any())
{
//--Table Header Declaration--
<div class="wrapper">
<div class="scrollTable">
<table class="table table-hover table-md ">
<thead>
<tr>
<th class="text-left tableHead d-none">Id</th>
<th class="text-left tableHead">Company</th>
<th class="text-left tableHead">User Name</th>
<th class="text-left tableHead">First Name</th>
<th class="text-left tableHead">Last Name</th>
<th class="text-left tableHead">Title</th>
<th class="text-left tableHead">City</th>
<th class="text-left tableHead">State</th>
<th class="text-left tableHead text-right">Remove</th>
</tr>
</thead>
@*--Table Body For Each to pull DB records--*@
<tbody>
@foreach (var user in Model)
{
@Html.Partial("~/Views/Administration/Users/UserTable.cshtml", user)
}
</tbody>
</table>
</div>
</div>
}
<div id="modal-placeholder"></div>
</div>
如果要显示用户在 EditUser.cshtml
上编辑前的 Company
,可以对 ApplicationUser
模型、EditUser 方法和下拉列表进行以下修改如下所示:
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Title { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public int CompanyId { get; set; }
[ForeignKey("CompanyId")]
public Company Company { get; set; }
}
EditUser 方法
var model = new EditUserViewModel
{
Id = user.Id,
Email = user.Email,
UserName = user.UserName,
FirstName = user.FirstName,
LastName = user.LastName,
Title = user.Title,
Address = user.Address,
City = user.City,
State = user.State,
CompanyId = user.CompanyId
};
公司下拉列表
<div class="col-sm-8">
@Html.DropDownListFor(model => model.CompanyId, new SelectList(Model.CompanyLists.OrderBy(x => x.CompanyName), "CompanyID", "CompanyName"), "-- Select Company --", new { @class = "form-control" })
<span asp-validation-for="CompanyId" class="text-danger"></span>
</div>