3

well since I never got an answer to my question here: checkboxes and radio buttons in MVC FormCollection

I thought I'd take a different approach and just find my radio button value client side, and pass it as a different name/value pair in my ajax call...it was a great idea, but I can't get it to work.

using this works fine to pass my form collection:

formCollection = $(':input');
        $.ajax({
            type: "POST",
            url: "/mycontroller/mymethod",
            data: formCollection,
            dataType: "text",
            success: showConfirm,
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr.responseText);
            }
        });

but when I try and change my data to an object like this:

formCollection = $(':input');
        $.ajax({
            type: "POST",
            url: "/mycontroller/mymethod",
            data: ({collection: formCollection}),
            dataType: "text",
            success: showConfirm,
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr.responseText);
            }
        });

it won't fly. I need to do that so ultimately I can use this:

formCollection = $(':input');
        $.ajax({
            type: "POST",
            url: "/mycontroller/mymethod",
            data: ({collection: formCollection, radiobutton: radiobuttonValue}),
            dataType: "text",
            success: showConfirm,
            error: function (xhr, textStatus, errorThrown) {
                alert(xhr.responseText);
            }
        });

my action method on the controller looks like this:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult mymethod(FormCollection collection)
        {
}

any ideas why when I change the data, it doesn't work?

4
  • what does FormCollection actually contain? -- see also api.jquery.com/jQuery.ajax/#sending-data-to-server Commented Sep 13, 2011 at 14:46
  • formCollection = $(':input'); it contains all of the input fields on the page... Commented Sep 13, 2011 at 14:52
  • 1
    So you're trying to submit a jQuery object, instead of data? Try using formCollection = $('form').serialize() instead. api.jquery.com/serialize Commented Sep 13, 2011 at 14:57
  • 1
    ok serializing the formCollection did the trick. Note to others though, it still can't be passed like data: ({ whatever: whatever })...it has to be passed like data: whatever.serialize(). If you make a "real" answer out of this mblase, I'll mark it as the answer. Commented Sep 13, 2011 at 15:11

2 Answers 2

2

Try using formCollection = $('form').serialize() instead.

http://api.jquery.com/serialize

This will produce a string that looks like a GET query string, which you can use directly in your first code sample.

Sign up to request clarification or add additional context in comments.

Comments

0

//first try and get the value of the field , that resolve the issue formCollection = $(':input').val();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.