2016-05-15 9 views
0

私はGetレジスタにユーザ役割が設定されたViewbagを持っています。そして私のビューでは、すべてのユーザのドロップダウンを表示します。誰かが役割を選択すると、私はaspNetUserRolesに、ユーザからと役割からIDを保存したいが、何とか私はコントローラに戻って私のドロップダウンリストから結果を渡す傾ける理由を知らない:S私はこれを持ってviewbagの結果を表示に渡す

登録ゲットコントローラ

public ActionResult Register() 
{ 
    ViewBag.Roles = new SelectList(db.Roles, "Id", "Name"); return View(); 
} 

登録ビュー

<div class="form-group"> 
    @Html.Label("Tipo de utilizador", new { @class = "col-md-2 control-label" }) 
    <div class="editor-field"> 
     @Html.DropDownList("Role", ViewBag.Roles as SelectList, "Please Select", new { @class = "form-control" }) 
    </div> 
</div> 

登録は

if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Sobre = model.Sobre, Idade = model.Idade, Telemóvel = model.Telemóvel, Nome = model.Nome }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      var roleStore = new RoleStore<IdentityRole>(db); 
      var roleManager = new RoleManager<IdentityRole>(roleStore); 

      var userStore = new UserStore<ApplicationUser>(db); 
      var userManager = new UserManager<ApplicationUser>(userStore); 


      if (result.Succeeded) 
      { 
       userManager.AddToRole(user.Id, "Role"); 
       await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

       // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
       // Send an email with this link 
       // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
       // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
       // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

       return RedirectToAction("Index", "Home"); 
      } 
      AddErrors(result); 
     } 

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

を取得し、私はどれだけ私はそれを行うことができ、RoleIdを受信し、それに対するユーザの仲間との役割データベースに追加したいですか?そして、私は見る

<div class="form-group"> 
    @Html.Label("Tipo de utilizador", new { @class = "col-md-2 control-label" }) 
    <div class="editor-field"> 
     @Html.DropDownListFor(x=>x.RoleId, new SelectList(ViewBag.Roles, "Id", " Name"), "Please Select", new { @class = "form-control" }) 
</div> 

レジで

public class ApplicationUser 
{ 
    public int RoleId{get;set;} 
    ///////Remain Column Here in ApplicationUser model 
} 

としてのあなたのモデルを変更aspNetUserRoleaspNetRole

+0

あなたは '' Role "'という名前のドロップダウンリストを持っていますので、ビューモデルが 'int Role'というプロパティを持っていると仮定すると、そのプロパティは選択された値に束縛されます。 –

答えて

0

のようにasp.netによって作成されたデフォルトのテーブルを使用したい

を取得
if (ModelState.IsValid) 
    { 
     var user = new ApplicationUser { UserName = model.Email, Email = model.Email, Sobre = model.Sobre, Idade = model.Idade, Telemóvel = model.Telemóvel, Nome = model.Nome }; 
     var result = await UserManager.CreateAsync(user, model.Password); 
     var roleStore = new RoleStore<IdentityRole>(db); 
     var roleManager = new RoleManager<IdentityRole>(roleStore); 

     var userStore = new UserStore<ApplicationUser>(db); 
     var userManager = new UserManager<ApplicationUser>(userStore); 


     if (result.Succeeded) 
     { 
      userManager.AddToRole(user.Id, model.RoleId); 
      await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 

      // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
      // Send an email with this link 
      // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); 
      // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); 
      // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); 

      return RedirectToAction("Index", "Home"); 
     } 
     AddErrors(result); 
    } 

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