0
votes

I am new to the MVC framework as well as ASP.NET so I apologize for any sloppy code.

I am creating an application that requires an Admin to set the roles of new users that register. When the Admin logs into the application, they are automatically directed to an Admin page that displays a list of users that have registered with the app. The admin has the ability to choose the role for the new user.

Here is my controller for the admin:

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");
    }

Here is the corresponding view:

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

}


Thank you in advance for taking the time to look at this question and any help that you can provide.

2
What is the problem that you are facing? - AgentFire
So, there is a question? - Andrey Gubal
I apologize if the question was not clear. The question is how do I add user to roles by using check-boxes? How can I link the roles to individual check-boxes in MVC 4? Thank You! - user2690539

2 Answers

1
votes

You can use Html.DropDownList helper for this. Firstly you will need to prepare your roles collection in controller to populate it. Here is the sample code:

[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);
}

Then in your view:

@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" /> 
}

You can also use Html.RadioButton helper in such way:

@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" /> 
}

If you want to make multiple roles selected at the same time, I suggest using some jQuery plugin, such as jQuery.chosen and Html.ListBox helper.

0
votes

Use EditorTemplates for all enums (Create "Role" enum):

@model Enum 
@Html.DropDownListFor(m => Enum.GetValues(Model.GetType()).Cast<Enum>().Select(m => 
new SelecteListItem {Selected = "your logic", Text = "", Value = ""}))

Or use custom partial view by current enum.