如何发布表单数据API控制器

编程入门 行业动态 更新时间:2024-10-27 08:33:45
本文介绍了如何发布表单数据API控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

表单数据发布使用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控制器

本文发布于:2023-11-15 09:22:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593169.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   控制器   数据   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!