0

UPDATE: This problem is probably due to my lack of understanding of the SPA template and techniques. I tried to ignore the upshot library and did a lot of manual modifications to the template prior to this error. Please vote to close as non-constructive.

I'm trying out ASP.Net MVC4 SPA Projects with real-world type scenarios. Since upshot is just not there yet, i'm using just the WebAPI part and Knockout with JQuery Ajax methods on the client side.

Everything was ok until I had to post data with arbitrary parameters:

This is my controller:

public class ProductLineController : DbDataController<WebSiteContext>
{
    [HttpPost]
    public HttpResponseMessage<Order> AddProductLine(int productId, int orderId)
    {
         [invoke logic to add the product line]
         [return the updated order]
    }
}

The client code to invoke this controller is on a Knockout viewmodel as follows:

this.addProduct = function (product) {
    var prodId = product.ProductId;

    var requestUrl = baseUrl + "productline";
    $.ajax({
        url: requestUrl,
        cache: false,
        type: 'POST',
        data: JSON.stringify({productId: prodId, orderId: orderId}),
        dataType:"json",
        contentType: 'application/json; charset=utf-8',
        success: function (data) {
            self.order(data);
        }
    });

Very straightforward. I took the precaution to stringify the json parameter data after some investigation on similar issues.

The Request Headers as reported by Fiddler are:

POST http://localhost:50045/api/orderedproduct HTTP/1.1
Host: localhost:50045
Connection: keep-alive
Content-Length: 27
Origin: http://localhost:50045
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) [snipped]
Content-Type: application/json; charset=UTF-8
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:50045/Order/Create
Accept-Encoding: gzip,deflate,sdch
Accept-Language: es-ES,es;q=0.8,en-US;q=0.6
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

{"productId":1,"orderId":1}

As you notice, the JSON is correctly formatted. But the response is a killer 500

System.Runtime.Serialization.SerializationException
There was an error deserializing the object of type System.Int32. The value '' cannot be parsed as the type 'Int32'.

Of curse the stacktrace is way longer than that, but this is the important part.

Can you tell me what is my mistake here? I'm just lost.

David Lay
  • 2,956
  • 4
  • 27
  • 48
  • It might be helpful to see the stack trace to see exactly where the exception is thrown. Are you sure that the exception occurs because of the parameters? Could not it be the return of the order? – Julien Jacobs May 14 '12 at 08:51
  • I'm sure is the parameters, a breakoint on the first line of the method `AddProductLine` is never reached, the exception occurs before the method is called by the framework. – David Lay May 20 '12 at 22:59
  • This answered it for me: http://stackoverflow.com/questions/10984040/post-parameter-is-always-null – Yannick Smits Aug 24 '12 at 20:15

0 Answers0