3

How can I get all ListBox values (not just the selected items) upon submit in an asp.net MVC2 project?

I'm using Ajax forms like Ajax.BeginForm("ActionName", new...).

I have already tried to select all the items in the OnBegin event of the Ajax options but not all of the ListBox items are being POSTed to the controller.

Sefer K
  • 504
  • 1
  • 5
  • 19

2 Answers2

2

This code works for me!

<script type="text/javascript">
$(document).ready(function () {

    $("#myForm").submit(function (e) {

        $("#myList option").prop("selected", "selected");

    });
}); 
</script>
0

This was my solution.

HTML:

<input type="submit" value="Save Changes" onmouseover="SelectAllItems()" />

JavaScript:

function SelectAllItems() {
    $("#UnlinkedProp").each(function() { 
        $("#UnlinkedProp option").attr("selected", "selected"); 
    }); 

    $("#LinkedProp").each(function() { 
        $("#LinkedProp option").attr("selected", "selected"); 
    }); 

    $("#UnlinkedProp").focus(); 

    $("#LinkedProp").focus();
}
Sefer K
  • 504
  • 1
  • 5
  • 19
  • hey ..what is SelectAllItems()? – Null Pointer Feb 11 '11 at 04:25
  • its a javascript function like below; `function SelectAllItems() { $("#UnlinkedProp").each(function() { $("#UnlinkedProp option").attr("selected", "selected"); }); $("#LinkedProp").each(function() { $("#LinkedProp option").attr("selected", "selected"); }); $("#UnlinkedProp").focus(); $("#LinkedProp").focus(); }` – Sefer K Feb 18 '11 at 08:18
  • I would suggest changing the `onmouseover` event to `onsubmit`. This way, it can handle situations like submission over hitting the enter key and clicking the button with keyboard selection. – Alpha Dec 08 '12 at 20:20