我有一个视图,显示用户名和他们所属的组。 如果我切换用户组,我该如何将用户和新组发送给控制器?
查看:
<table>
<tr>
<td>user ID</td>
<td>user group</td>
<td>Actions</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.UserName</td>
<td>@Html.DropDownList("grup", new SelectList(ViewBag.GroupList, "ID", "UserGroupName"), item.UserGroupName)</td>
<td>@Html.ActionLink("Save","EditUserGroup", UserInGroup)</td>
</tr>
}
</table>
Viewbag.GroupList是{ID,GroupName}
的列表控制器
public ActionResult EditUserGroup(UserInGroup userInGroup, string grup)
{
}
如何使用所有详细信息发送所选组和用户的值?
修改
我将模型更改为:
public class UserInGroupModel
{
public IList<UserInGroup> userInGroupList { get; set; }
public int GroupId { get; set; }
}
控制器
public ActionResult UserGroup()
{
IUserGroupService userGroupService = new UserGroupService();
ViewBag.GroupList = userGroupService.GetEntities();
var lista = userInGroupService.GetEntities();
UserInGroupModel userInGroupModel = new UserInGroupModel();
userInGroupModel.userInGroupList = lista;
return View(userInGroupModel);
}
查看:
@using (Html.BeginForm("EditUserGroup", "Admin"))
{
<table>
<tr>
<td>user ID</td>
<td>user group</td>
<td>Actions</td>
</tr>
@foreach (var item in Model.userInGroupList)
{
<tr>
<td>@item.UserName</td>
<td>@Html.DropDownListFor(model => model.GroupId, new SelectList(ViewBag.GroupList, "ID", "UserGroupName"))</td>
<td><input type="submit" value="Save"/></td>
</tr>
}
</table>
}
我仍然无法发送UserInGroup的值。我可以在控制器的groupId参数中获取GroupId,但是不传递UserInModel实体。你能救我吗?
答案 0 :(得分:1)
使用dropdownlistfor
@Html.DropDownListFor(x => x.grup, new SelectList(ViewBag.GroupList, "ID"...
为您所选项目的模型添加一个变量(在本例中为grup)