如何将下拉列表的值与模型一起发送?

时间:2022-11-07 04:05:21

I have a view that displays user names with groups they are part of. In case i switch the group of a user, how can i send bot the user and the new group to the controller ?

我有一个视图,显示用户名和他们所属的组。如果我切换用户组,我如何将用户和新组发送到控制器?

View :

查看:

<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 is a list of {ID, GroupName}

Viewbag.GroupList是{ID,GroupName}的列表

Controller

调节器

public ActionResult EditUserGroup(UserInGroup userInGroup, string grup)
{
}

How can i send the value of the selected group and the user with all the details ?

如何使用所有详细信息发送所选组和用户的值?

Edit

编辑

I changed the Model to :

我将模型更改为:

public class UserInGroupModel
{
    public IList<UserInGroup> userInGroupList { get; set; } 

    public int GroupId { get; set; }

}

Controller

调节器

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

View:

视图:

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

}

}

I still cannot send the value of the UserInGroup. I can get the GroupId in the groupId parameter of the controller but, the UserInModel entity is not passed. Can you help me ?

我仍然无法发送UserInGroup的值。我可以在控制器的groupId参数中获取GroupId,但是不传递UserInModel实体。你可以帮我吗 ?

1 个解决方案

#1


1  

use dropdownlistfor

使用dropdownlistfor

@Html.DropDownListFor(x => x.grup, new SelectList(ViewBag.GroupList, "ID"...

add a variable to your model for the selected item (grup in this case)

为您选择的项目的模型添加一个变量(在这种情况下为grup)

#1


1  

use dropdownlistfor

使用dropdownlistfor

@Html.DropDownListFor(x => x.grup, new SelectList(ViewBag.GroupList, "ID"...

add a variable to your model for the selected item (grup in this case)

为您选择的项目的模型添加一个变量(在这种情况下为grup)