2012-04-15 42 views
1

我得到我的賬戶控制器上以下錯誤:廣東話添加角色自動

The role 'roleName' was not found. 

但我有管理兩個角色,和遊戲玩家

下拉在我的註冊視圖列表挑選他們:

<label for="roleName">Select Role:</label> 
    @Html.DropDownList("roleName") 
    @Html.ValidationMessage("roleName") 

在我的帳戶控制器我有以下幾點:

// 
     // GET: /Account/Register 
     [AllowAnonymous] 
     public ActionResult Register() 
     { 
      ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName"); 
      return View(); 
     } 

     // 
     // POST: /Account/Register 

     [HttpPost] 
     [AllowAnonymous] 
     public ActionResult Register(RegisterModel model) 
     { 
      if (ModelState.IsValid) 
      { 
       // Attempt to register the user 
       MembershipCreateStatus createStatus; 
       Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus); 

       if (createStatus == MembershipCreateStatus.Success) 
       { 
        Roles.AddUserToRole(model.UserName, "roleName"); 
        FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */); 
        return RedirectToAction("Index", "Home"); 
       } 

       else 
       { 
        ModelState.AddModelError("", ErrorCodeToString(createStatus)); 
       } 
      } 

      // If we got this far, something failed, redisplay form 
      return View(model); 

     } 

我不知道這是爲什麼happing?

回答

1

貌似問題是在這裏:

Roles.AddUserToRole(model.UserName, "roleName"); 

我猜你不是故意寫「角色名」的文字。也許model.RoleName或「用戶」,或存在於你的角色提供一個角色?

+0

我不明白你的答案?你是告訴我改變某些東西或指出我有不正確的代碼對不起,我不是粗魯的,只是不明白你的回答對不起 – user1137472 2012-04-15 01:09:55

+0

噢好吧我明白你想讓我改變model.username到model.rolename我會做到這一點讓你知道結果感謝您的回答 – user1137472 2012-04-15 01:11:21

+0

model.rolename不存在,當我改變它therfore下一個選項是model.username,並把用戶名括號將嘗試,讓你知道謝謝 – user1137472 2012-04-15 01:13:18