I am working on an administrative panel that will allow a user to add one or many other users. There is a text area where the admin can input one or many user IDs to be added to the application, which corresponds to a user's ID assigned during the employment process. On submission, the application pulls name, email, etc from a database containing all employees and displays it on the screen for verification. Also included on the screen are some checkboxes to assign some permissions, such as CanWrite and IsAdmin.
View
using (Html.BeginForm())
{
<table>
<tr>
<th>
</th>
<th>
@Html.DisplayNameFor(model => model.User.First().ID)
</th>
<th>
@Html.DisplayNameFor(model => model.User.First().Name)
</th>
<th>
@Html.DisplayNameFor(model => model.User.First().Email)
</th>
<th>
@Html.DisplayNameFor(model => model.User.First().CanWrite)
</th>
<th>
@Html.DisplayNameFor(model => model.User.First().IsAdmin)
</th>
</tr>
@foreach (var item in Model.User)
{
<tr>
<td>
<input type="checkbox" name="id" value="@item.ID" checked=checked/>
</td>
<td>
@Html.DisplayFor(modelItem => item.ID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.CanWrite)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsAdmin)
</td>
</tr>
}
</table>
<input type="submit" />
}
NOTE: The reason for the checkbox with name ID is to give the ability to not add a user once the name and other information has been fetched, for example, a user you didn't mean to add got in the list of IDs by accident.
Model
public class User
{
public int ID { set; get; }
public string Name { set; get; }
public bool IsAdmin { set; get; }
public bool CanWrite { set; get; }
public string Email{ set; get; }
}
Controller
[HttpPost]
public ActionResult Create(IEnumerable<User> model)
{
//With this code, model shows up as null
}
In the case of a single user, I know I can use User model as the parameter in my controller action. How can I adjust this code to work with multiple users being added at once? Is this even possible?