We have a form where a user can move items from one multi-select box to another. We are using MVC 4.5 and jquery validate with Microsoft's unobtrusive javascript.
The problem is, when submitting the form, the values within the select boxes don't get submitted because the user doesn't know after moving items that they have to also select all those items for submission.
So the solution sounds easy: use jquery to select all items upon submit.
After doing some research (thanks stackoverflow community), we were able to discover the necessary jquery code to intercept the submit process. However, the problem arises in that when the code selects the items, only pre-existing items in a select box are selected. Live (dynamic) items that have been moved do not get selected.
Here is the code that we first used:
$('#UserProfileForm').bind('invalid-form.validate',function(){
$(this).find('.selectbox option').each(function (i) {
$(this).attr("selected", "selected");
});
});
We discovered that using bind doesn't work but live should:
$('#UserProfileForm').live('invalid-form.validate',function(){
$(this).find('.selectbox option').each(function (i) {
$(this).attr("selected", "selected");
});
});
However, we are using jquery 1.9 and the live function has been removed. It has been replaced with this:
$(document).on('invalid-form.validate', 'form', function () {
$(this).find('.selectbox option').each(function (i) {
$(this).attr("selected", "selected");
});
});
However, this still doesn't work. So I replaced the selection function with an alert to see if it works at all:
$(document).on('invalid-form.validate', 'form', function () {
alert('test');
});
An alert does not pop up. Any ideas?
ADDITIONAL INFO
For those wondering why I'm referencing invalid-form.validate is because I am testing with invalid form data. The same scenario would apply if it was valid form data. I just haven't gotten to the point on how to bind live data to the valid form submission process either.