C# MVC 5将所有列表框项传递给控制器

C# MVC 5将所有列表框项传递给控制器,c#,jquery,asp.net-mvc,model-view-controller,listbox,C#,Jquery,Asp.net Mvc,Model View Controller,Listbox,我正在尝试将列表框中的项目传递回控制器。 我的控制器总是空的。如果我为它声明另一个变量作为列表,它只会传回所选的值。我想传回所有的值 我有两个列表框,我使用jquery在它们之间传输项目 $('#btnRight').click(function (e) { var selectedOpts = $('#CurrentRoles option:selected'); if (selectedOpts.length == 0) { alert("Nothing t

我正在尝试将列表框中的项目传递回控制器。 我的控制器总是空的。如果我为它声明另一个变量作为列表,它只会传回所选的值。我想传回所有的值

我有两个列表框,我使用jquery在它们之间传输项目

$('#btnRight').click(function (e) {

    var selectedOpts = $('#CurrentRoles option:selected');
    if (selectedOpts.length == 0) {
        alert("Nothing to move.");
        e.preventDefault();
    }
    $('#ListOfRoles').append($(selectedOpts).clone());
    $(selectedOpts).remove();
    e.preventDefault();
});
$('#btnLeft').click(function(e) {

    var selectedOpts = $('#ListOfRoles option:selected');

    if (selectedOpts.length == 0) {

        alert("Nothing to move.");

        e.preventDefault();

    }

    $('#CurrentRoles').append($(selectedOpts).clone());

    $(selectedOpts).remove();
    e.preventDefault();
});
这是我的查看代码

@Html.ListBox("CurrentRoles", new SelectList(Model.CurrentRoles, "Key", "Value"), new { id = "CurrentRoles" })

 public ActionResult Edit(UserRolesmanagement role,List<string> CurrentRoles)
{

   //logic
}
@Html.ListBox(“CurrentRoles”,新建SelectList(Model.CurrentRoles,“Key”,“Value”),新建{id=“CurrentRoles”})
公共操作结果编辑(用户角色管理角色,列表当前角色)
{
//逻辑
}
模型

public用户角色管理()
{
user=newapplicationuser();
ListOfRoles=新列表();
CurrentRoles=新字典();
ListOfUsers=新列表();
}       
公共应用程序用户{get;set;}
公共字典CurrentRoles{get;set;}
公共列表ListOfRoles{get;set;}
公共列表列表用户{get;set;}

事实上,表单数据中的1个输入只能向控制器传输1个值。 例如:

@using (Html.BeginForm("Cons", "Report"))
{ 
    <input name="start" id="start" value="1234" />           
    <input name="end" id="end" value="2345" />
}
因此,如果我想传输值列表,最简单的方法是添加输入,所有这些值用逗号分隔。大概是这样的:

<input id="CurrentRolesValues" name="CurrentRolesValues" value="" style="display:none"/>
控制器

public ActionResult Edit(UserRolesmanagement role, string CurrentRolesValues)
{
   List<string> roles = CurrentRolesValues.Split(',').ToList(); 
   //logic
}
公共操作结果编辑(用户角色管理角色,字符串CurrentRolesValues) { 列表角色=CurrentRolesValues.Split(',').ToList(); //逻辑 }
<input id="CurrentRolesValues" name="CurrentRolesValues" value="" style="display:none"/>
var value="";
$('#ListOfRoles').children().each(function () {
    value+=$(this).val()+","
});
value=value.slice(0, -1); //delete last comma
$('#CurrentRolesValues').val(value);   
public ActionResult Edit(UserRolesmanagement role, string CurrentRolesValues)
{
   List<string> roles = CurrentRolesValues.Split(',').ToList(); 
   //logic
}