【发布时间】:2013-06-14 21:41:06
【问题描述】:
我有一个名为 Roles 的表,其中包含三个字段
Guid RoleIdstring RoleNamestring Description
在我的register.cshtml 视图中,我想要一个dropdownlist,它显示Roles 表中的RoleName 列表。我还需要能够获得该值并使用它,例如将角色分配给用户,这将在控制器中完成。
我的视图目前如下所示,我将模型用作AspNetUser,但它不了解Role,这是我想在dropdownlist. 中展示的内容
@model Sorama.CustomAuthentiaction.Models.AspNetUser
@{
ViewBag.Title = "Register";
Layout = "~/Views/shared/_BootstrapLayout.empty.cshtml";
}
@section Styles{
<link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" type="text/css" />
}
<div class ="form-signin">
@using (Html.BeginForm("Register", "Account"))
{
@Html.ValidationSummary(true)
<h2 class="form-signin-heading"> Register </h2>
<div class ="input-block-level">@Html.TextBoxFor(model=>model.Email, new{@placeholder = "Email"})</div>
<div class ="input-block-level">@Html.TextBoxFor(model=>model.UserName, new{@placeholder = "UserName"})</div>
<div class ="input-block-level">@Html.PasswordFor(model=>model.Password, new{@placeholder ="Password"})</div>
<div class ="input-block-level">@Html.DropDownListFor(//don't know what to do
<button class="btn btn-large btn-primary" type="submit">Register</button>
}
</div>
我的控制器是这样的
public class AccountController : Controller
{
//private readonly IDbContext dbContext;
//
// GET: /Account/
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginModel model)
{
if(Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
[HttpGet]
public ActionResult Register()
{
string [] roles = Roles.GetAllRoles();
return View(roles);
}
[HttpPost]
public ActionResult Register(AspNetUser model)
{
return View();
}
public ActionResult Index()
{
return View();
}
}
我需要做什么才能拥有该下拉列表?
【问题讨论】:
标签: asp.net-mvc-4 drop-down-menu