[英]Migration from ASP.NET MVC to ASP.NET Core MVC - action model binding resulting null
While migrating from ASP.NET MVC to ASP.NET Core MVC, I faced binding behavior troubles which I couldn't overcome.从 ASP.NET MVC 迁移到 ASP.NET Core MVC 时,我遇到了无法克服的绑定行为问题。
I have this controller (for Razor, not API):我有这个 controller(对于 Razor,不是 API):
public class SomeController : Controller
{
[HttpPost]
public IActionResult SomeAction([FromBody] SomeModel model)
{
return Ok(model?.SomeValue);
}
}
public class SomeModel
{
public int SomeValue { get; set; }
}
If I make a request to /some/someAction
with a body of { "SomeValue": "6970" }
, I get a model equals null.如果我使用{ "SomeValue": "6970" }
的主体向/some/someAction
发出请求,我会得到一个 model 等于 null。
If I remove quotes { "SomeValue": 6970 }
I receive what I need, the model isn't null.如果我删除引号{ "SomeValue": 6970 }
我收到我需要的东西,model 不是 null。
The project is big and I cant change all the js code to ints, also it could be a problem to change all models which expect int.该项目很大,我无法将所有 js 代码更改为 int,更改所有期望 int 的模型也可能是一个问题。
How I can let it work easily?我怎样才能让它轻松工作?
Try to change your model from int to string and check if it works尝试将您的 model 从 int 更改为 string 并检查它是否有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.