简体   繁体   English

使用 post 方法向 asp.net 核心 3 mvc 动作发送多个参数

[英]send multiple parameters to asp.net core 3 mvc action using post method

There is problem in sending ajax requests that have multiple parameters to asp.net mvc core 3 action using http post method.使用 http 发布方法将具有多个参数的 ajax 请求发送到 asp.net mvc 核心 3 操作时出现问题。 the parameters do not bind.参数不绑定。 In dot net framework asp.net web api there was similar limitation but not in asp.net mvc actions.在点网框架 asp.net web api 中存在类似的限制,但在 Z1848E7322214CCE460E72B 中没有类似的限制。 I want to know is there work around this in asp.net core 3 mvc or is this the new limitation?我想知道在 asp.net 核心 3 mvc 中是否有解决此问题的方法,或者这是新的限制? action:行动:

public string SomeAction([FromBody]string param1, [FromBody]IEnumerable<SomeType> param2, [FromBody]IEnumerable<SomeType> param3)
{
       //param1 and param2 and param3 are null
}

client:客户:

    $.ajax({
        contentType: 'application/json',
        data: JSON.stringify({
            "param1": "someString",
            "param2": someList,
            "param3": someList
        }),
        type: "POST",
        dataType: "json",
        url: "/SomeController/SomeAction",
        success: function (result) {
        },
        error: function (error) {
            console.error(error);
        }
    }
    );

With the new version actions need to be explicit about what and where they expected to bind models from.在新版本中,操作需要明确说明他们希望从什么以及从哪里绑定模型。

Create a mode to hold all the required data创建一个模式来保存所有需要的数据

public class SomeActionModel {
    public string param1 { get; set; }
    public IEnumerable<SomeType> param2 { get; set; }
    public IEnumerable<SomeType> param3 { get; set; }
}

Update the action to expect the data from the body of the request更新操作以期望来自请求正文的数据

public IActionResult SomeAction([FromBody] SomeActionModel model) {
    if(ModelState.IsValid) {
        string param1 = model.param1;
        IEnumerable<SomeType> param2 = model.param2;
        IEnumerable<SomeType> param3 = model.param3;

        //...

        return Ok();
    }

    return BadRequest(ModelState);
}

The client should also send the data in the correct format客户端还应该以正确的格式发送数据

var model = {
    param1: GeometricNetworkTrace_Class.flags,
    param2: GeometricNetworkTrace_Class.barriers,
    param3: feederIds
};

$.ajax({
    contentType: 'application/json',
    data: JSON.stringify(model),
    type: "POST",
    dataType: "json",
    url: "/controllerName/actionName",
    success: function (result) {
    },
    error: function (error) {
        console.error(error);
    }
});

Reference Model Binding in ASP.NET Core参考Model 绑定在 ASP.NET 内核中

The reason for a single [FromBody] parameter is described in Parameter Binding in ASP.NET Web API .单个[FromBody]参数的原因在 ASP.NET Web API 中的参数绑定中描述。 The request body might be stored in a non-buffered stream that can only be read once.请求正文可能存储在只能读取一次的非缓冲 stream 中。

With a custom model binder you can get around it.使用定制的 model 活页夹,您可以绕过它。 See Posting JavaScript types to MVC 6 in .NET core, using Ajax .请参阅使用 Ajax 在 .NET 内核中将 JavaScript 类型发布到 MVC 6

Try adding [FromBody] attribute in your controller signature.尝试在您的 controller 签名中添加[FromBody]属性。

public string SomeAction([FromBody] string param1, IEnumerable<SomeType> param2, IEnumerable<SomeType> param3)
{
       //param1 and param2 and param3 are null
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET Core 2.1 MVC 使用 XMLHttpRequest 将数据从 JavaScript 发送到 Action 方法 - ASP.NET Core 2.1 MVC send data from JavaScript to Action method using XMLHttpRequest ASP.NET MVC - 带有URL附加参数的POST操作方法 - ASP.NET MVC - POST Action Method with Additional Parameters from URL AJAX Post to ASP.NET MVC Controller操作方法-空参数 - AJAX Post to ASP.NET MVC Controller action method - Null parameters ASP.NET Core MVC添加的Edit()动作方法(POST)脚手架为什么ID参数传了两次? - Why is the ID parameter passed twice in the Edit() action method (POST) scaffold added by ASP.NET Core MVC? 我的帖子表单没有达到我的 asp.net mvc 核心 web 应用程序中的操作方法 - My Post form is not reaching the action method inside my asp.net mvc core web application 将发布参数传递给ASP.NET MVC中的操作方法? - Passing post parameters to action methods in asp.net mvc? asp.net 核心 3 Web Api ! 将 json 发送到 POST 操作 - asp.net core 3 Web Api ! send json to POST Action ASP.NET 核心 MVC 过滤:如何将多个值发送给操作? - ASP.NET Core MVC filtering: How can I send multiple value to action? ASP.NET MVC 4 - 使用post方法 - ASP.NET MVC 4 - using post method 通过Fiddler发送POST请求到asp.net mvc动作 - Send POST request to asp.net mvc action via Fiddler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM