【发布时间】:2018-06-06 08:16:17
【问题描述】:
我在将具有属性的对象传递给控制器时遇到问题。
两者始终为空。
在 console.log 中传递的数据看起来不错。我哪里做错了?
我研究了其他解决方案,但没有什么能把我推向正确的方向......
C#、MVC5、Jquery 2.1.1
控制器:
public class Position
{
string intnew { get; set; }
string intlast { get; set; }
}
行动:
[HttpPost]
public ActionResult change(Position position)
{
return Json(new { result = "" });
}
并查看:
var pos = new Object;
pos.intnew = '22';
pos.intlast = '11';
$.ajax({
url: '/szkolenia/change',
contentType: 'application/json; charset=utf-8',
datatype: 'json',
data: JSON.stringify({ position: pos }),
type: "POST",
success: function (_infoLogs) {
console.log(JSON.stringify({ position: pos }));
}
});
【问题讨论】:
-
因为你的属性不是
public -
耶稣....非常感谢!!!!
-
Spehen Muecke 所说的 + [FromBody] 位置,我认为
-
@WhoMightThisOneBe,
[FromBody]是 web-api,而不是 mvc -
[FromBody] 仅适用于我所知道的 API。
标签: c# json ajax asp.net-mvc