0

This is my class ARecipe :

public class ARecipe
{
    public string picture { get; set; }
    public string title { get; set; }
    public int cookingTime { get; set; }
    public int preparationTime { get; set; }
    public string IngredientList { get; set; }
    public string ingredientsDescription { get; set; }
    public int nbPersons { get; set; }
    public string Category { get; set; }
    public string difficulty { get; set; }
    public double nbStars { get; set; }

}

My Ajax call :

var dico = { 
            picture: $("#fakeInput").val(),
            title : $("#title").val(),
            cookingTime : $("#cookingTime").val(),
            preparationTime : $("#preparationTime").val(),
            IngredientList : $("#ingredientListArea").val(),
            ingredientsDescription : $("#preparationArea").val(),
            nbPersons : parseInt($("#select-nb-Persons").val()),
            Category : $("#select-category").val(),
            difficulty: $("#select-difficulty").val(),
            nbStars : 4
        };

        $.ajax({
            url: "/AddRecipe/TempData",
            type: 'POST',
            success: function (e) {
                //success event
            },
            ///Form data
            data: JSON.stringify(dico),
            ///Options to tell JQuery not to process data or worry about content-type
            cache: false,
            contentType: false,
            processData: false
        });

And the method receiving the datas :

 [HttpPost]
  public ActionResult TempData(ARecipe recipe) {

     return Json("");
  }

My Ajax call well go to the TempData method but when I analyse the parameter 'recipe' with the debugger, I notice that all the fields are 'null'.

Why ?

Do you have a solution ?

Thank you

2
  • 1
    Specify dataType: 'json' and contentType: 'application/json', Commented Aug 10, 2014 at 9:39
  • can you try just data: dico,? Commented Aug 10, 2014 at 9:41

2 Answers 2

1

You are sending the data as JSON, but the server expects is as regular POST data. Just let the ajax method turn it into a regular POST request instead of forcing it into JSON:

///Form data
data: dico,
Sign up to request clarification or add additional context in comments.

Comments

0

Just Correct these issues :

 $.ajax({
        url: "/AddRecipe/TempData",
        type: 'POST',
        dataType: 'json',
        contentType: 'application/json',
        success: function (e) {
            //success event
        },
        ///Form data
        data: JSON.stringify(dico),
        ///Options to tell JQuery not to process data or worry about content-type
        cache: false,
    });

 [HttpPost]
 public JsonResult TempData(ARecipe recipe) {

 return Json("");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.