4

I am trying to post to an ASP.NET MVC action from another Action (separate sites) and I need to post a collection of objects. I'm unable to find how to serialize this collection of objects so I can get them as a standard NameValueCollection. Example:

            var test1 = new TestObject { FirstName = "John", LastName="Smith", IDNum=12345 };
            var test2 = new TestObject { FirstName = "Betty", LastName="Jones", IDNum=34567};
            var test3 = new TestObject { FirstName = "Bobby", LastName="Hebert", IDNum=9876 };

            List<TestObject> coll;
            coll.Add(test1);
            coll.Add(test2);
            coll.Add(test3);

            WebClient wc = new WebClient();
            wc.UploadData("http://mysite.com",  ??? );
            // or
            wc.UploadValues("http://mysite.com", ??? );
            // or...
            // ?????

Any help would be appreciated. Thanks in advance.

Jorin
  • 1,652
  • 1
  • 19
  • 25

1 Answers1

-1

Just do this -

wc.Headers["Content-type"] = "application/x-www-form-urlencoded";

wc.UploadString("url", "postData");

Maxim
  • 7,268
  • 1
  • 32
  • 44
  • I appreciate finally getting an answer on this. I actually don't even remember the scenario anymore its been so long :). Either way, this will definitely work. The question was more focused on how to do it with a collection of items, but I can certainly just loop through/creatively join those and concatenate the names/values into a string in the format of FirstName[0]=John&LastName[0]=Smith&IDNum[0]=12345&FirstName[1]=Betty&LastName[1]=Jones&IDNum[1]=34567... and it should work. Thanks. – Jorin Dec 16 '10 at 14:57