tags:

views:

126

answers:

2

Hi Guys

I'm developing an ASP.NET MVC app and I'm a bit curious if anyone can tell me if there's a better way than this to submit selected options with the rest of the form data?

var lstOption1 = $('#lstOption1 :selected').attr("optionId1");
var lstOption2= $('#lstOption2 :selected').attr("optionId2");

var formData = $(this).serialize()
    + "&lstOption1 =" + lstOption1 + 
    + "&lstOption2=" + lstOption2;

$.post($(this).attr("action"), formData, function(res)
{

});

Thanks

Dave

+1  A: 
$('select#mySelect').attr('value');

This will give you the selected option.

idrumgood
+1  A: 

I have an MVC page that needs to submit the selected value from a group of radio buttons.

I use:

var data = $.makeArray($("input[type=radio]").serializeArray());

To make an array of their names and values

and then post it with jQuery's ajax() to the MVC controller

$.ajax({
    url: "/Rounding.aspx/Round/" + $("#OfferId").val(),
    type: 'POST',
    dataType: 'html',
    data: $.toJSON(data), // <-- jQuery plug in to convert to json string
    contentType: 'application/json; charset=utf-8',
    beforeSend: doSubmitBeforeSend,
    complete: doSubmitComplete,
    success: doSubmitSuccess
});

Which sends the data across as native JSON data.

You can then capture the response stream and de-serialize it into the native C#/VB.net object and manipulate it in your controller.

To automate this process in a lovely, low maintenance way, I advise reading this entry that spells out most of native, automatic JSON de-serialization quite well.

Article on MVC JSON deserialization

... and because Stack OVerflow won't let me post 2 links because I'm "new" you will have to google "jquery-json google code" to find the jQuery JSON plug-in I'm using.

I hope this helps!

Evildonald
The JSON link is: http://code.google.com/p/jquery-json/ha! Take that StackOverflow overlords!
Evildonald