serializing form data to a web api

is there a better way of implementing this code:

public Payment SendPaymentToSagePay(Payment paymentModel)

        var webClient = new WebClient()
            BaseAddress = "http://localhost:64317/api/PaymentStart"

        string price = paymentModel.price.ToString();

        string productId = paymentModel.productId.ToString();

        string paymentMode = paymentModel.paymentMode.ToString();

        string context = paymentModel.context.ToString();

        var collection = new NameValueCollection();
        collection.Add("title", paymentModel.title);
        collection.Add("firstName", paymentModel.firstName);
        collection.Add("lastName", paymentModel.lastName);
        collection.Add("context", context);
        collection.Add("programmeName", paymentModel.programmeName);
        collection.Add("productId", productId);
        collection.Add("price", price);
        collection.Add("cardHolderTitle", paymentModel.cardHolderTitle);
        collection.Add("cardHolderLastName", paymentModel.cardHolderLastName);
        collection.Add("cardHolderFirstName,", paymentModel.cardHolderFirstName);
        collection.Add("cardHolderEmail", paymentModel.cardHolderEmail);
        collection.Add("selfAddress1", paymentModel.selfAddress1);
        collection.Add("selfAddress2", paymentModel.selfAddress2);
        collection.Add("selfCity", paymentModel.selfCity);
        collection.Add("selfCountry", paymentModel.selfCountry);
        collection.Add("selfState", paymentModel.selfState);
        collection.Add("selfPhone", paymentModel.selfPhone);
        collection.Add("selfPostCode", paymentModel.selfPostCode);
        collection.Add("otherAddress1", paymentModel.otherAddress1);
        collection.Add("otherAddress2", paymentModel.otherAddress2);
        collection.Add("otherCity", paymentModel.otherCity);
        collection.Add("otherCountry", paymentModel.otherCountry);
        collection.Add("otherState", paymentModel.otherState);
        collection.Add("otherPhone", paymentModel.otherPhone);
        collection.Add("otherPostCode", paymentModel.otherPostCode);
        collection.Add("ipAddress", paymentModel.ipAddress);
        collection.Add("additionalInfo", paymentModel.additionalInfo);
        collection.Add("paymentMode", paymentMode);

        byte[] responseBytes = webClient.UploadValues("", "POST", collection);
        string response = Encoding.UTF8.GetString(responseBytes);
        string decodedResponse = HttpUtility.UrlDecode(response);
        JavaScriptSerializer js = new JavaScriptSerializer();
        var payment = js.Deserialize<Payment>(decodedResponse);
        return payment;

I am using this method to serialize my payment model and then send it to an external web api which then adds this data to various repositories.

Is there a more succinct method of doing this?

Ideally I want a solution that's more MVC4 and Dependency Injection (I am currently using the Unity container) friendly, I created this serializer as an interim solution whilst I researched other methods, so far this is the only one I have been able to successfully implement.

Any advice would be very much appreciated.


Can you post JSON instead of a form collection? If you can, you can use the same JavaScriptSerializer you're using to deserialize the response to serialize your Payment:

string serializedPayment = js.Serialize(paymentModel);

Need Your Help

Linux Rename command uppercase first letter

regex linux bash sed rename

I'm writing a Bash script for cleaning up in my music.

Skip ahead in Windows Media Player embedded webpage object

javascript internet-explorer windows-media-player

I have an embedded Windows Media Player object in my website and I'd like to make it skip ahead to a certain point when the page loads. Most sites suggest using: