我试图按照this example。
这里是我的C#代码:
public class MyModel
{
int? ID { get; set; }
}
public class MyResourceController : ApiController
{
[HttpPost]
public MyModel MyPostAction(MyModel model)
{
return model;
}
}
这里是我的JavaScript:
var data = { model: { ID: 1 } };
$http.post(
'/api/MyResource/MyPostAction',
JSON.stringify(data),
{
headers: {
'Content-Type': 'application/json'
}
}
);
当我在动作设置断点和model.ID
是null
而不是1
。我如何发布一个复杂的对象?
您可以使用标题中的[FromBody]跳过字符串化。请参阅http://stackoverflow.com/questions/27869763/how-to-post-an-object-to-webapi-using-angularjs/27869884#27869884 –