gpt4 book ai didi

c# - MVC4 无法将对象属性从 AJAX POST 映射到 JSON

转载 作者:太空宇宙 更新时间:2023-11-03 11:03:54 24 4
gpt4 key购买 nike

型号:

public class JsonRequest
{
public string Data { get; set; }
}

行动:

[HttpPost]
public ActionResult Index(JsonRequest data)
{
return new JsonResult()
{
Data = string.Format("Data: {0}", data.Data), // data.Data == null here
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}

Ajax :

$.ajax({
type: 'POST',
url: '@Url.Action("Index", "Home")',
cache: false,
data: JSON.stringify({ data: "Hello World!" }),
success: function(data) {
alert(data);
}
});

JsonRequest 对象在 Index 操作中有一个实例,但它的 Data 属性未映射到传递的 JSON。我怎样才能做到这一点?

最佳答案

你需要删除 JSON.stringify() 调用,因为 jQuery 会自己做。根据标准,最好编写 {"Data": "Hello world"}(引号中的“Data”)。

关于c# - MVC4 无法将对象属性从 AJAX POST 映射到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16688979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com