我有一个RegisterViewModel,它接收用户的输入并将其存储在数据库中。我已经使用IEnumerable在dropdownlist中存储角色。视图工作正常但我无法获取下拉列表值并将其存储到用户表中。我该怎么做才能获得下拉列表值?
public class RegisterViewModel
{
[Required]
[DisplayName("User Name")]
public string UserName { get; set; }
[Required]
[Remote("EmailAlreadyExists", "Validation", ErrorMessage = "User with this mail exists")]
[DisplayName("Email")]
public string Email { get; set; }
[Required]
[DisplayName("Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm Password")]
public string ConfirmPassword { get; set; }
[Required]
public IEnumerable<SelectListItem> Roles { get; set; }
}
我的角色模型
public class Role
{
public int Id { get; set; }
public string Name { get; set; }
}
查看角色是,
<div class="form-group">
@Html.LabelFor(model => model.Roles, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Roles, Model.Roles, "Select Roles" , new {id = "Roles" })
@Html.ValidationMessageFor(model => model.Roles, "", new { @class = "text-danger" })
</div>
</div>
最后我的控制器
public ActionResult Index()
{
RegisterViewModel uvm = new RegisterViewModel();
uvm.Roles = from r in roles
select new System.Web.Mvc.SelectListItem
{
Text = r.Name,
Value = r.Id.ToString()
};
return View(uvm);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(RegisterViewModel register)
{
if (ModelState.IsValid)
{
string code = Guid.NewGuid().ToString();
userRepo.Insert(new Models.User()
{
UserName = register.UserName,
Email = register.Email,
Password = register.Password,
/* Role = register.Roles */
CreatedDate = DateTime.Now,
AuthCode = code,
Status = false
});
SendMail(register.UserName, register.Email, code);
return RedirectToAction("Thankyou");
}
return View(register);
}
答案 0 :(得分:0)
在RegisterViewModel中添加新属性
public class RegisterViewModel
{
[Required]
[DisplayName("User Name")]
public string UserName { get; set; }
[Required]
[Remote("EmailAlreadyExists", "Validation", ErrorMessage = "User with this mail exists")]
[DisplayName("Email")]
public string Email { get; set; }
[Required]
[DisplayName("Password")]
public string Password { get; set; }
[Required]
[DataType(DataType.Password)]
[DisplayName("Confirm Password")]
public string ConfirmPassword { get; set; }
[Required]
public IEnumerable<SelectListItem> Roles { get; set; }
**public string SelectedRole {get;set;}**
}
和下拉应该像bewlo
@Html.DropDownListFor(model => model.SelectedRole , Model.Roles, "Select Roles" , new {id = "Roles" })
您的控制器操作看起来像
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(RegisterViewModel register)
{
if (ModelState.IsValid)
{
string code = Guid.NewGuid().ToString();
userRepo.Insert(new Models.User()
{
UserName = register.UserName,
Email = register.Email,
Password = register.Password,
Role = register.SelectedRole
CreatedDate = DateTime.Now,
AuthCode = code,
Status = false
});
SendMail(register.UserName, register.Email, code);
return RedirectToAction("Thankyou");
}
return View(register);
}