如何在控制器中获取Listbox值

时间:2014-06-09 08:17:46

标签: c# asp.net-mvc asp.net-mvc-4

我在视图中有两个列表框(注意:请参阅随附的屏幕截图)。选择轮次(listbox1)和轮次列表(listbox2)。我选择并将我的选择移动到第二个列表框。现在我需要在控制器中获取第二个列表框值并将第二个列表框的所有选定值插入到db.How来执行此操作?

型号:

 public string[] listval { get; set; }

查看:

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

Refer View

1 个答案:

答案 0 :(得分:1)

尝试以下脚本

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