C# 使用MVC4ASP.NET中的复选框将用户添加到角色

C# 使用MVC4ASP.NET中的复选框将用户添加到角色,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我对MVC框架和ASP.NET都是新手,所以我为任何草率的代码道歉 我正在创建一个需要管理员设置注册新用户角色的应用程序。当管理员登录到应用程序时,他们会自动转到一个管理员页面,该页面显示已向应用程序注册的用户列表。管理员可以为新用户选择角色 这是我的管理员控制器: public class AdminTestController : Controller { private UsersContext db = new UsersContext(); // GET: /Admi

我对MVC框架和ASP.NET都是新手,所以我为任何草率的代码道歉

我正在创建一个需要管理员设置注册新用户角色的应用程序。当管理员登录到应用程序时,他们会自动转到一个管理员页面,该页面显示已向应用程序注册的用户列表。管理员可以为新用户选择角色

这是我的管理员控制器:

public class AdminTestController : Controller
{
    private UsersContext db = new UsersContext();

    // GET: /AdminTest/
    [Authorize(Roles = "Admin")]
    public ActionResult Index()
    {

        var model = db.UserProfiles.ToList();
        return View(model);
    }

    [HttpPost]
    public ActionResult Submit(string userName, string selectedRole)
    {
        Roles.AddUserToRole(userName,selectedRole);
        return View("Index");
    }
以下是相应的视图:

@model IEnumerable<WAP.Models.UserProfile>

@{
   ViewBag.Title = "Index";
}

...

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.UserId)
    </td>
    <td>

    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UserName)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
        @Html.ActionLink("Details", "Details", new { id=item.UserId }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.UserId })

        @using (Html.BeginForm("Submit", "AdminTest", FormMethod.Post))
        {

            <select name ="selectedRole">
              <option value="Null"></option>
              <option value="Manager">Manager</option>
              <option value="Agent">Agent</option>
            </select>

            <input id="SubmitChange" type="submit" value="Submit" /> 
            <input id="userName" type ="text" value= "@item.UserName" name= "userName" hidden ="hidden" />
        }
    </td>
</tr>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
...
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.UserId)
@DisplayFor(modelItem=>item.UserName)
@ActionLink(“编辑”,“编辑”,新的{id=item.UserId})|
@ActionLink(“详细信息”,“详细信息”,新的{id=item.UserId})|
@ActionLink(“删除”,“删除”,新的{id=item.UserId})
@使用(Html.BeginForm(“Submit”、“AdminTest”、FormMethod.Post))
{
经理
代理人
}
}



提前感谢您抽出时间查看此问题以及您可以提供的任何帮助。

您可以使用Html.DropDownList帮助程序来完成此操作。首先,您需要在controller中准备角色集合以填充它。以下是示例代码:

[Authorize(Roles = "Admin")]
public ActionResult Index()
{
    var model = db.UserProfiles.ToList();
    var rolesCollection = new List<string> {"Null", "Manager", "Agent"};
    ViewBag.Roles = new SelectList(rolesCollection);
    return View(model);
}
[Authorize(Roles=“Admin”)]
公共行动结果索引()
{
var model=db.UserProfiles.ToList();
var rolesCollection=新列表{“Null”、“Manager”、“Agent”};
ViewBag.Roles=新选择列表(rolesCollection);
返回视图(模型);
}
那么在你看来,

@using (Html.BeginForm("Submit", "AdminTest", FormMethod.Post))
{
    @Html.Hidden("userName", item.UserName)
    @Html.DropDownList("selectedRole", (SelectList)ViewBag.Roles)

    <input id="SubmitChange" type="submit" value="Submit" /> 
}
@使用(Html.BeginForm(“Submit”、“AdminTest”、FormMethod.Post))
{
@Html.Hidden(“用户名”,item.userName)
@DropDownList(“selectedRole”,(SelectList)ViewBag.Roles)
}
您还可以通过以下方式使用Html.RadioButton助手:

@using (Html.BeginForm("Submit", "AdminTest", FormMethod.Post))
{
    @Html.Hidden("userName", item.UserName)

    @Html.RadioButton("selectedRole", "Null", true)
    @Html.RadioButton("selectedRole", "Manager")
    @Html.RadioButton("selectedRole", "Agent")

    <input id="SubmitChange" type="submit" value="Submit" /> 
}
@使用(Html.BeginForm(“Submit”、“AdminTest”、FormMethod.Post))
{
@Html.Hidden(“用户名”,item.userName)
@RadioButton(“selectedRole”、“Null”、true)
@单选按钮(“selectedRole”、“Manager”)
@单选按钮(“selectedRole”、“代理”)
}

如果您想同时选择多个角色,我建议使用一些jQuery插件,例如andHtml.ListBoxhelper。

对所有枚举使用EditorTemplates(创建“角色”枚举):

@模型枚举
@Html.DropDownListFor(m=>Enum.GetValues(Model.GetType()).Cast().Select(m=>
新的SelecteListItem{Selected=“your logic”,Text=“”,Value=“”}))

或按当前枚举使用自定义局部视图

你面临的问题是什么?那么,有一个问题吗?如果问题不清楚,我道歉。问题是如何使用复选框将用户添加到角色?如何将角色链接到MVC4中的各个复选框?非常感谢。
@model Enum 
@Html.DropDownListFor(m => Enum.GetValues(Model.GetType()).Cast<Enum>().Select(m => 
new SelecteListItem {Selected = "your logic", Text = "", Value = ""}))