简体   繁体   English

如何在控制器中获取列表框值

[英]How to get Listbox values in controller

I have Two listboxes in view ( Note:Refer the attached Screenshot ).choose rounds(listbox1) and list of rounds(listbox2).I select and move my choices to second listbox.Now I need to get second list box values in controllers and insert that all selected values of second listbox into db.How to do this? 我在视图中有两个列表框( 注:请参考所附的屏幕快照)。选择回合(listbox1)和回合列表(listbox2)。选择并将其移动到第二个列表框。现在我需要在控制器中获取第二个列表框值将第二个列表框的所有选定值插入db。如何做到这一点?

Model: 模型:

 public string[] listval { get; set; }

View: 视图:

<tr>
                <td align="right">
                    <p id="text" style="width: 90px;">Rounds</p>
                </td>
                <td>
                    @Html.ListBox("lstBox1", new List<SelectListItem>{ 
                     new SelectListItem() {Text = "--Choose Rounds--", Value="None"},
                     new SelectListItem() {Text = "GD", Value="GD"}, 
                     new SelectListItem() {Text = "Written", Value="Written"}, 
                     new SelectListItem() {Text = "Technical1", Value="Technical1"}, 
                     new SelectListItem() {Text = "Technical2", Value="Technical2"},
                     new SelectListItem() {Text = "HR", Value="HR"} 
                    })
                </td>
                <td style='width: 50px; text-align: left; vertical-align: middle;'>
                    <input type='button' id='btnRight' value='  >  ' />
                    <br />
                    <input type='button' id='btnLeft' value='  <  ' />
                </td>
                <td>
                    @Html.ListBoxFor(m=>m.listval, new List<SelectListItem>{ new SelectListItem() {Text = "--List Of Rounds--"} })
                </td>
            </tr>


<script>
    $('#btnRight').click(function (e) {
        var selectedOpts = $('#lstBox1 option:selected');
        if (selectedOpts.length == 0) {
            alert("Nothing to move.");
            e.preventDefault();
        }

        $('#listval').append($(selectedOpts).clone());
        $(selectedOpts).remove();
        e.preventDefault();
    });

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

        $('#lstBox1').append($(selectedOpts).clone());
        $(selectedOpts).remove();
        e.preventDefault();
    });
</script>

参考检视

Try below script 尝试以下脚本

  <script> $('#btnRight').click(function (e) { var selectedOpts = $('#lstBox1 option:selected'); if (selectedOpts.length == 0) { alert("Nothing to move."); e.preventDefault(); } $('#listval').append($(selectedOpts).clone()); $(selectedOpts).remove(); SelectList(); e.preventDefault(); }); $('#btnLeft').click(function (e) { var selectedOpts = $('#listval option:selected'); if (selectedOpts.length == 0) { alert("Nothing to move."); e.preventDefault(); } $('#lstBox1').append($(selectedOpts).clone()); $(selectedOpts).remove(); SelectList(); e.preventDefault(); }); //Here I am using SelectList() function to select options..of second listbox function SelectList() { $("#listval option").each( function (index, option) { //Store the option if (index != 0) $(option).attr("selected", "selected"); } ); } </script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM