[英]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.