C# 如何在asp.net mvc 4中从SelectList中获取项目
我正在创建一个自定义的注册表单,只供网站管理员使用。在这个注册表单中,管理员可以添加用户并赋予他们在网站中的角色,所以我创建了这个模型来定义fromC# 如何在asp.net mvc 4中从SelectList中获取项目,c#,asp.net,asp.net-mvc,list,razor,C#,Asp.net,Asp.net Mvc,List,Razor,我正在创建一个自定义的注册表单,只供网站管理员使用。在这个注册表单中,管理员可以添加用户并赋予他们在网站中的角色,所以我创建了这个模型来定义from public class AddUserToRoleModels { [Required] [Display(Name = "User name")] public string UserName { get; set; } [Required] [StringLength(100, ErrorMessag
public class AddUserToRoleModels
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
[Required]
[Display(Name = "Select Role")]
public string Roles { get; set; }
}
并使该方法在类函数中添加用户并赋予他们角色
public class Functions
{
public void AddUserToRole(string UserName,string PassWord,string Role) {
if (!WebSecurity.UserExists(UserName))
WebSecurity.CreateUserAndAccount(UserName, PassWord);
if (!Roles.GetRolesForUser(UserName).Contains(Role))
Roles.AddUsersToRoles(new[] { UserName }, new[] { Role });
}
}
然后我创建这个控制器
public class AddUserToRoleController : Controller
{
UsersContext db = new UsersContext();
public ActionResult Create()
{
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View();
}
[HttpPost]
public ActionResult Create(AddUserToRoleModels model)
{
if (ModelState.IsValid)
{
Functions ob = new Functions();
ob.AddUserToRole(model.UserName, model.Password, model.Roles);
return RedirectToAction("Index");
}
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View(model);
}
}
using (UsersContext db = new UsersContext()){
var tmp = db.Roles.ToList();
ViewBag.RoleId = new SelectList(tmp, "RoleId", "RoleName");
}
并为创建动作控制器创建视图
@model SeniorProject.Models.AddUserToRoleModels
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>AddUserToRoleModels</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Roles)
</div>
<div class="editor-field">
@Html.DropDownList("RoleId", String.Empty)
@Html.ValidationMessageFor(model => model.Roles)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
当我尝试添加新用户并给他角色时,用户插入到数据库中,但没有角色,我认为因为我试图从@Html.DropDownListRoleId获取角色,String.Empty,它是list and my method,它为角色使用字符串参数,我尝试从此列表中获取所选项目,但无法工作,因此请帮助
@Html.DropDownListFor(model => model.RoleID, ViewBag.RoleID, "")
或者类似的东西
您也可以将该列表作为selectList放到viewmodel中,然后将其称为Model。RoleIDs试试以下方法:
public ActionResult Create()
{
ViewBag.Roles = new SelectList(db.Roles, "RoleId", "RoleName");
return View();
}
鉴于:
@Html.DropDownListFor(model => model.RoleId, ViewBag.Roles as IEnumerable<SelectListItem>, "Select a role" )
在操作后ifmodel.RoleId==0,因此您不选择任何角色。在控制器中
public class AddUserToRoleController : Controller
{
UsersContext db = new UsersContext();
public ActionResult Create()
{
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View();
}
[HttpPost]
public ActionResult Create(AddUserToRoleModels model)
{
if (ModelState.IsValid)
{
Functions ob = new Functions();
ob.AddUserToRole(model.UserName, model.Password, model.Roles);
return RedirectToAction("Index");
}
ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
return View(model);
}
}
using (UsersContext db = new UsersContext()){
var tmp = db.Roles.ToList();
ViewBag.RoleId = new SelectList(tmp, "RoleId", "RoleName");
}
您需要将RoleID绑定到int字段。在Ryan的回答中,dropdownlist值将以编写代码的方式绑定到模型属性int RoleId,如果将int RoleId添加到创建方法的参数列表中,它将绑定到该属性。在创建httppost操作中,角色id是否与模型一起发布?