将viewbag结果传递给视图

时间:2016-05-15 13:51:29

标签: asp.net-mvc asp.net-mvc-4

我有一个Viewbag,在我的Get寄存器中填充了用户角色,然后在我的视图中显示所有用户的下拉列表。当有人选择我想要保存id的角色,用户以及从角色到aspNetUserRoles,但不知何故我不知道为什么我不能将结果从我的下拉列表传递回我的控制器:S

我有这个:

注册获取控制器

 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并将其添加到角色数据库中,并将用户关联到它,我该怎么做?我想使用asp.net创建的默认表,如aspNetUserRoleaspNetRole

1 个答案:

答案 0 :(得分:0)

将模型修改为

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

观看

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

注册获取

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);
}
相关问题