Jorin
Jorin

Reputation: 1662

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.

Upvotes: 4

Views: 1624

Answers (1)

Maxim
Maxim

Reputation: 7348

Just do this -

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

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

Upvotes: -1

Related Questions