Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a collection to MVC controller via jquery

I'm using ASP.NET MVC3 with Jquery. I'm trying to pass my form elements back to the controller using something like this (Please note I removed success and error code for simplicity):

var formElements = $("#myForm").serialize();
        $.ajax({
            type: "POST",
            url: ScriptResolveUrl("~/Report/SubmitChanges"),
            data: {collection: formElements},
            success: 
            error:
            dataType: "json"
        });

My question is what should the parameter in my controller method look like: Here is my controller method:

public ActionResult SubmitChanges(WHAT GOES HERE?)
{
}

So what I'm really looking for is what should be the type of the parameter going into the controller method? I want to be able to retrieve the values of the form elements in the controller.

like image 615
carlg Avatar asked Nov 28 '25 03:11

carlg


1 Answers

So here is what I did. I have about 20-30 elements on my form so I really didn't want to have to turn each one into a parameter or list them all out in the collection.

In the jquery, I did the following:

 var formElements = $("#myForm").serialize();

        $.ajax({
            type: "POST",
            url: ScriptResolveUrl("~/Report/SubmitChanges"),
            data: { parms: formElements },
            success:
            error: 
            dataType: "json"
        });

It then goes into my controller as a string:

public ActionResult SubmitChanges(string parms)

I then found a function to parse that string (seems to be working)

NameValueCollection qscoll = HttpUtility.ParseQueryString(parms);

This seems to work without listing out all of the form elements.

like image 65
carlg Avatar answered Nov 30 '25 18:11

carlg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!