Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am making a tool in which one can get a Quotation throw javascript and then send himself that quotation. The making of the quotation is going fine but when sending the email, the quotation data is corrupt.
The data in the Quotation object is fine except for the Options array. When sending 3 array items the Options array holds 3 items except for their names are null and the price is 0.

The quotation is send to ASP.NET MVC 3 using jQuery.post.

Quotation object in C# looks like:

public class Quotation
{
    public string Email { get; set; }
    public string Product { get; set; }
    public int Amount { get; set; }
    public decimal BasePrice { get; set; }
    public decimal SendPrice { get; set; }
    public Option[] Options { get; set; }
    public decimal Discount { get; set; }
    public decimal SubTotal { get; set; }
    public decimal TotalPrice { get; set; }
}
public class Option
{
    public string Name { get; set; }
    public decimal Price { get; set; }
}

The Action Method looks like:

[HttpPost]
public JsonResult Offerte(Models.Quotation quotation)
{
    //
}

The jQuery looks like:

$.post(baseUrl + "/api/Offerte/", jsonContent, function (data) {
    alert(data.Message);
});

The jsonContent object looks like:

{
    "Options":[
        {
            "Name":"Extra pagina's (16)",
            "Price":40
        },
        {
            "Name":"Papier Keuze",
            "Price":30
        },
        {
            "Name":"Omslag",
            "Price":29.950000000000003
        }
    ],
    "Amount":"5",
    "BasePrice":99.96000000000001,
    "SubTotal":199.91000000000003,
    "SendPrice":0,
    "Discount":19.991,
    "TotalPrice":179.91900000000004,
    "Email":"[email protected]"
} 

Does anyone know why the array is not set properly?


EDIT
If I add this debug code to the controller:

using (var writer = System.IO.File.CreateText(Server.MapPath("~/App_Data/debug.txt")))
{
    writer.AutoFlush = true;

    foreach (var key in Request.Form.AllKeys)
    {
        writer.WriteLine(key + ": " + Request.Form[key]);
    }
}

Options[0][Name]: Extra pagina's (52)
Options[0][Price]: 156
Options[1][Name]: Papier Keuze
Options[1][Price]: 68.4
Options[2][Name]: Omslag
Options[2][Price]: 41.94
Amount: 6
BasePrice: 149.91899999999998
SubTotal: 416.25899999999996
SendPrice: 0
Discount: 45.78848999999999
TotalPrice: 370.47051
Email: [email protected]

This means that the data does get to the controller but the Options still doesn't get set right. And I don't want an easy fix that I parse it myself afterwards, I want to know the correct way to handle it so MVC will take care of it.

share|improve this question
"The quotation is send to ASP.NET MVC 3 using jQuery.post." why you have this restriction? Can you use jQuery.ajax instead? – nemesv Jan 12 at 12:40
Looks like in your Quotation class, you're defining the options array to hold options rather than objects. – Derek Jan 12 at 12:40
Or you need to override the set function to parse each option object and specifically set each property of the Option objects. – Derek Jan 12 at 12:43
@nemesv not a restriction, just a choice. – SynerCoder Jan 12 at 12:43
@Derek That seems to be the problem. They are not parsed good. But do you know how to do that? – SynerCoder Jan 12 at 12:44
show 4 more comments

1 Answer

up vote 1 down vote accepted

If you want to send JSON data to an ASP.NET MVC controller action and you want the model binding work currently (e.g. binding collections on your model) you need to specify the contentType as "aplication/json".

Because with the $.post you are not able to specify the contentType you need to use $.ajax and you also need to JSON.stringify your data:

$.ajax({
    url: baseUrl + "/api/Offerte/",
    type: 'POST',
    data: JSON.stringify(jsonContent),
    contentType: "application/json",
    success: function (data) {
        alert(data.Message);
    }
});
share|improve this answer
The fourth parameter if the .post() function is the content type. Using 'json' for that value would work – Derek Jan 12 at 12:52
@Derek no the fourth parameter is the data type: dataType Type: String The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html). which is different than then contentType. – nemesv Jan 12 at 12:55
@nemesv The client side is not the problem. The problem is how MVC parses/reads the data. As you can read in my question everything is received fine EXCEPT for the array. – SynerCoder Jan 12 at 12:59
@SynerCoder have you tried out my solution? I know that only the array is not working but MVC handles the collections differently that is why the rest is working already. If still not working you can try with data: JSON.stringify(jsonContent), – nemesv Jan 12 at 13:00
@nemesv I just did to be sure, but not working. Like I said the clientside is not the problem. It is how MVC parses the data. – SynerCoder Jan 12 at 13:04
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.