将数据数组从Javascript传递到C# - c#

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

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

ajax:从json数组中提取json对象 - javascript

JSON(来自api的内部数组):[{"id":"001", "name":"john", "age":"40"}, {"id":"002", "name":"jane&…

改造正在返回一个空的响应主体 - java

我正在尝试使用Retrofit和Gson解析一些JSON。但是,我得到的响应机构是空的。当我尝试从对象中打印信息时,出现NullPointerException。我确保URL正确,并且我也确保POJO也正确。我正在使用jsonschema2pojo来帮助创建POJO类。这是我要解析的JSON{ "?xml": { "@versi…

从数据表C#创建复杂的JSON - javascript

我的数据集中有3个数据表。表A与B和C有一对多的关系。我想像下面那样在C#中使用Linq创建Json。有人可以帮帮我吗?我要在此先感谢所有将指导我或为我的问题提供投入的人。{ "A": [ { "id": "0001", "type": "donut", &#…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …