这是我的类(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; }
}
我的 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,
关于c# - 将数据数组从 Javascript 传递到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25227311/