Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 添加注册时的用户角色(表单身份验证)MVC3_Asp.net Mvc 3_Forms Authentication - Fatal编程技术网

Asp.net mvc 3 添加注册时的用户角色(表单身份验证)MVC3

Asp.net mvc 3 添加注册时的用户角色(表单身份验证)MVC3,asp.net-mvc-3,forms-authentication,Asp.net Mvc 3,Forms Authentication,我正在开发一个MVC3项目,希望在用户注册时使用表单身份验证将其添加到角色中。因此,我想创建一些复选框,或者一个下拉列表,显示所选择的角色,并在注册时将用户分配给该角色 到目前为止,我有以下代码: public ActionResult Register() { ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName"); return View(); }

我正在开发一个MVC3项目,希望在用户注册时使用表单身份验证将其添加到角色中。因此,我想创建一些复选框,或者一个下拉列表,显示所选择的角色,并在注册时将用户分配给该角色

到目前为止,我有以下代码:

    public ActionResult Register()
    {
        ViewData["roleName"] = new SelectList(Roles.GetAllRoles(), "roleName");
        return View();
    }
我认为:

<label for="roleName">Select Role:</label>
@Html.DropDownList("roleName")
@Html.ValidationMessage("roleName")
因此,所有角色都会显示在视图中。我所需要知道的就是如何添加到HttpPost部分以使其正常工作

非常感谢,艾米

if (createStatus == MembershipCreateStatus.Success)
            {
                Roles.AddUserToRole(model.UserName, "RoleName");
                FormsAuthentication.SetAuthCookie(model.UserName, false /* createPersistentCookie */);
                return RedirectToAction("Index", "Home");
            }
试试这个。这应该是可行的

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