[RESOLVED]Can I get form data exactly as they posted?

say I have form posted as

$.ajax({
...
data: [
{name:'id', value:'1'}, {name:'name', value:'John'}, {name:'desc', value:'John's bio'},
{name:'id', value:'2'}, {name:'name', value:'Mary'}
{name:'id', value:'3'}, {name:'name', value:'Susan'}, {name:'desc', value:'Susan's bio'},
],
})

In action, I got FormCollection as

form["id"] = "1,2,3"
form["name]= "John, Mary, Susan"
form["desc"] = "John's Bio, Susan's Bio"

But my target is get objects like

{ id = "1", name="John", desc="John's bio"}
{ id = "2", name="Mary", desc=null}
{ id = "3", name="Susan", desc="Susan's bio"}

Obviously, I couldn’t tell which desc field is missing from posted data, So my question is if there is way to get posted data as is what’s like I posted

The form collection does not allow duplicate keys, so in the cause of dups, the values are concat’d with commas. You can parse the post body yourself, or use unique names on the client side. Or use a model and Mvc binder.

So where could I got entire post body, is it one of properties of RequestContext object?

I suppose that you could manually populate a set of n objects for each value that you receive using some nasty String.Split() operations as seen below :

public ActionResult Index()
{
            // Get your IDs
            var ids = Request["id"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var names = Request["name"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            var descriptions = Request["desc"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            // List of your objects (using example class)
            List<YourClass> objects = new List<YourClass>();

            // Get the number of objects sent (assumes each will have an ID and build an object for each
            for (int i = 0; i < ids.Length; i++)
            {
                // For each of these build an object using the available indices from the String.Split()
                YourClass c = new YourClass();
                
                // Set the ID (if it exists)
                c.id = Convert.ToInt32(ids[0]);
                
                // If this index is available, use it's value
                c.name = (i < names.Length) ? names[i] : null;
                
                // Do the same for the descriptions
                c.desc = (i < descriptions.Length) ? descriptions[i] : null;

                // Store this in your collection
                objects.Add(c);
            }

            // Now you can see your objects here in the objects variable

            return View();
}

As you can see, you’ll create a very basic object to store all of your values, which might look something like this :

public class YourClass
{
        public int id { get; set; }
        public string name { get; set; }
        public string desc{ get; set; }

        public YourClass()
        {

        }
}

Ideally, if you have any control over how you are actually passing up your data, you might consider building objects that contain all of your properties and then use JSON to serialize the collection and read it in within your Controller Action using the
available binding within MVC :

public ActionResult Index(YourClass[] data)
{
     // Code omitted for brevity
}

There are numerous different strategies for handling this, depending on how much control you have over how your are passing your data in through AJAX.

use Request.InputStream. as the stream will be at eof, you will need to do a reset (seek(0))

            var list = new List<Tuple<string, string>>();
            Request.InputStream.Seek(0, 0);
            var s = (new StreamReader(Request.InputStream)).ReadToEnd();
            var p = s.Split('&');
            foreach (var v in p)
            {
                var nv = v.Split('=');
                list.Add(new Tuple<string, string>(HttpUtility.UrlDecode(nv[0]), HttpUtility.UrlDecode(nv[1])));
            }

to read it. then read into a string. do a split on "&" to get an array or parameters. then loop thru the the parameter array and split on "=" to get the name/value pair. you will then need to url decode the name & value. that it. just a couple lines of code.

Leave a Reply