【发布时间】:2019-12-02 15:51:59
【问题描述】:
我正在尝试清理一些乱七八糟的东西。我有一个正确填充的 Company 下拉列表。但我不知道如何让下拉菜单工作的选定值。我看过很多例子,但是在我们使用它的上下文中,这让我感到困惑。 我们正在使用 asp.Net Core Identity。
感谢任何指导!
下拉列表:
<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 或我需要做些什么来完成这项工作。
EditUserViewModel:
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>
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-core razor asp.net-identity