2016-12-27 6 views
0

リストボックスのアイテムをコントローラに戻しています。 私のコントローラは常に空です。 List.Itとして別の変数を宣言すると、選択した値だけが返されます。私はすべての値を返す。MVC 5はすべてのリストボックスアイテムをコントローラに渡します

私はそれらの間で項目を転送するためにjqueryを使用する2つのリストボックスを持っています。ここ

$('#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(); 
}); 

は実際にフォームデータ内の1つの入力がちょうど1値をコントローラに転送することができビュー

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

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

    //logic 
} 

モデル

public UserRolesmanagement() 
{ 
    user = new ApplicationUser(); 
    ListOfRoles = new List<IdentityRole>(); 
    CurrentRoles = new Dictionary<string, string>(); 
    ListOfUsers = new List<ApplicationUser>(); 
}  

public ApplicationUser user { get; set; } 
public Dictionary<string,string> CurrentRoles { get; set;} 
public List<IdentityRole> ListOfRoles { get; set; } 
public List<ApplicationUser> ListOfUsers { get; set; } 

答えて

0

ための私のコードです。たとえば :

@using (Html.BeginForm("Cons", "Report")) 
{ 
    <input name="start" id="start" value="1234" />   
    <input name="end" id="end" value="2345" /> 
} 

とコントローラ:私のため

public ActionResult Cons(string start, string end) 
{ 
    //blahblahblah 
} 

のでsimpest方法私は、値のリストを転送したい場合は、カンマで区切られたすべてこの値を使用して入力を追加しています。このような何か:入力への追加値のため

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

JS

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 
} 
関連する問題