2014-04-03 24 views
2

我正在創建一個自定義註冊表單,供本網站的管理員在本註冊表單中使用,管理員可以添加用戶並在網站中爲他們提供角色,這個模型從如何從asp.net中的SelectList中獲取項目4

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); 

    } 
} 

,併爲創建動作控制器

@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.DropDownList("RoleId", String.Empty),這是清單,我的方法是採取字符串參數的作用,我試圖從這個名單採取選定的項目,但沒有工作乙醚,請幫助作用...

+0

需要將角色ID綁定到一個int領域。在Ryan的回答中,下拉列表值將綁定到模型屬性'int RoleID' – CSharper

+0

您的代碼編寫方式,如果您將'int RoleId'添加到Create方法的參數列表中,它將綁定到該屬性。 – CSharper

+0

在創建httppost操作中,角色ID是否與模型一起發佈? –

回答

0
@Html.DropDownListFor(model => model.RoleID, ViewBag.RoleID, "") 

或類似的東西那。

而且你還可以把清單中的視圖模型作爲選擇列表的,然後只是把它作爲Model.RoleIDs

0

試試這個:

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") 

在發帖行動if(model.RoleId == 0),所以你不選擇任何角色。

-1

在控制器

 
using (UsersContext db = new UsersContext()){ 
    var tmp = db.Roles.ToList(); 
    ViewBag.RoleId = new SelectList(tmp, "RoleId", "RoleName"); 
} 

在查看

@Html.DropDownListFor(model => model.RoleID, (SelectList)@ViewBag.RoleId) 
相關問題