这是我的课程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; }

}


我的Ajax电话:

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
        });


以及接收数据的方法:

 [HttpPost]
  public ActionResult TempData(ARecipe recipe) {

     return Json("");
  }


我的Ajax调用很好地转到了TempData方法,但是当我使用调试器分析参数“ recipe”时,我注意到所有字段均为“ null”。

为什么呢

你有解决方案吗 ?

谢谢

最佳答案

您正在以JSON形式发送数据,但服务器希望将其作为常规POST数据发送。只需让ajax方法将其转换为常规POST请求,而不是将其强制为JSON:

///Form data
data: dico,

09-07 00:40
查看更多