我的模特为
public class UserToRolesModel
{
public List<ApplicationUser> userList { get; set; }
public List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> rolesList { get; set; }
[Required]
public List<Microsoft.AspNet.Identity.EntityFramework.IdentityRole> selectedRoles { get; set; }
[Required]
public List<ApplicationUser> selectedUsers { get; set; }
}
视图为
@model InventoryEasy15.Models.UserToRolesModel
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.ListBoxFor(x => x.selectedRoles, new SelectList(Model.rolesList, "Id", "Name"), new { style = "width:100%", id = "Roles", @class = "select2", data_placeholder = "Select the Role" })
@Html.ValidationMessageFor(m => m.selectedRoles, "", new { @class = "text-danger" })
我在Post中得到的模型是空的。我的帖子是
[HttpPost]
public ActionResult SaveUserRoles(UserToRolesModel usersroles)
{
foreach(var user in usersroles.selectedUsers)
{
var userExistingRoles = context.Roles;
}
return View(usersroles);
}
get正确绑定数据,
public ActionResult AddUserToRole()
{
var model = new UserToRolesModel();
var users = context.Users.ToList();
model.userList = users;
model.rolesList = context.Roles.ToList();
return View(model);
}
以前我遇到过问题,我将模型用于局部变量,导致了同样的问题。但我不认为这里有问题