问题描述
表单数据发布使用jQuery:
Form data is posted using jquery:
$.ajax('API/Validate/Customer?column=1&rowid=2&vmnr=3&isik=4',
{
data: JSON.stringify({
headerData: $("#_form").serializeArray()
}),
async: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
type: "POST"
});
这是由ASP.NET MVC4的Web API控制器收到的验证:
It is received by ASP.NET MVC4 Web API controller Validate:
public class ValidateController : ApiController
{
public class Body
{
public Dictionary<string, string> headerData { get; set; }
public Dictionary<string, string> rowData { get; set; }
}
public HttpResponseMessage Validate(
string id,
string column,
string rowid,
int? vmnr,
string isik,
[FromBody] Body body,
string dok = null,
string culture = null,
uint? company = null
)
{ ...
body.headerData值为null控制器中。
body.headerData value is null in controller.
据回答
How以接收网络API控制器Post方法动态数据
body.headerData必须有形式键。
然而,它是空的。
body.headerData must have form keys.However, it is empty.
如何获得headerData在控制器键,值对的?
How to get headerData as key, value pairs in controller ?
Chorme开发工具显示正确的JSON是贴在身上:
Chorme developer tools show that proper json is posted in body:
{"headerData":[{"name":"Kalktoode","value":"kllöklö"},
{"name":"Kaal","value":""}
]}
我试图删除
public Dictionary<string, string> rowData { get; set; }
从类,但问题仍然存在。
from class but problem persists.
推荐答案
事实上,你的控制器将反序列化的身体像:
Your controller do not match with what you are sending
Indeed, your controller will deserialize body like:
{
"headerData": {"someKey":"someValue", "otherKEy":"otherValue"},
"rowData": {"someKey":"someKey"}
}
和它的不可以的你实际发送JSON的结构。
您的控制器寻找一个机构,2个成员 beeing的 Dictionnaries 的,不是的键值对的数组的
And it is not the structure of the JSON you actually send.Your controller looks for a body with 2 members beeing Dictionnaries, not Arrays of key value pair.
通过键值阵,我的意思是这样的:
By array of key value, I mean something like:
{
"headerData": [
{
"key": "string",
"value": "string"
}
],
"rowData": [
{
"key": "string",
"value": "string"
}
]
}
您需要更新您的身体对象:
You need to update your Body object to:
[HttpPost, Route("test")]
public void Test(Body b)
{
}
public class Body
{
public List<KeyValuePair<string,string>> headerData { get; set; }
public List<KeyValuePair<string,string>> rowData { get; set; }
}
这篇关于如何发布表单数据API控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!