我正在一個管理面板,將允許用戶添加一個或許多其他用戶。有一個文本區域,管理員可以在其中輸入要添加到應用程序的一個或多個用戶ID,該用戶ID與就業過程中分配的用戶ID相對應。提交時,應用程序從包含所有員工的數據庫中提取姓名,電子郵件等,並將其顯示在屏幕上進行驗證。屏幕上還包含一些用於分配一些權限的複選框,如CanWrite
和IsAdmin
。模型綁定與多個實體
查看
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" />
}
注:之所以用名ID
複選框是給不添加用戶的能力,一旦名稱等信息已被提取,例如,一個用戶你並不意味着將意外添加到ID列表中。
型號
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; }
}
控制器
[HttpPost]
public ActionResult Create(IEnumerable<User> model)
{
//With this code, model shows up as null
}
在單用戶的情況下,我知道我可以使用User model
在我的控制器動作的參數。如何調整此代碼以便一次添加多個用戶?這甚至有可能嗎?
正是我的意思。好例子。 – meilke