简体   繁体   中英

how to Receive two ajax json Data from .net

i use .net and send js(ajax) two data to .net controller
in data test1 and test2 is json data.

.ajax({
        type: 'POST',
        url: "/test/test_Put/",
        contentType: 'application/json; charset=utf-8',
        data: {json_1:JSON.stringify(test1), json_2:JSON.stringify(test2)},
        dataType:'JSON',
        success:function(data){
        },
        error: function (data) {
        }
    });

.net controller

[HttpPost]
        public JsonResult test_Put([FromBody]test1 tt1, [FromBody]test2 tt2){
        }

but i saw error ㅜㅜ
how should i handle it in .net

You should only have 1 [FromBody] in your action. https://learn.microsoft.com/en-us/as.net/core/mvc/models/model-binding?view=as.netcore-2.1

There can be at most one parameter per action decorated with [FromBody]. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other [FromBody] parameters.

Create a wrapper class for this instead.

public class TestRequest
{
    public test1 json_1;
    public test2 json_2;
}

[HttpPost]
public JsonResult test_Put([FromBody]TestRequest data){}

BTW, you should use camel case in your method and variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM