asp.net-mvc-2webclientwebclient.uploaddata

Post collection of C# objects using WebClient


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.


Solution

  • Just do this -

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

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