简体   繁体   English

Asp.Net Core oData-发布请求的所有属性为null

[英]Asp.Net Core oData - Post request all properties are null

I try to setup up a oData controller for my Asp.Net core project. 我尝试为我的Asp.Net核心项目设置一个oData控制器。

Get/GetSingle/Delete/Patch is working like a charm. Get/GetSingle/Delete/Patch的工作Get/GetSingle/Delete/Patch很简单。

But my post request my incoming model always have all properties set to null. 但是我的帖子请求我的传入模型始终将所有属性设置为null。

I am using Asp.Net.Core 2.2 and oData 7.1 我正在使用Asp.Net.Core 2.2和oData 7.1

public async Task<ActionResult<TViewModel>> Post(TViewModel item)
{           
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    try
    {
        var addedEntity = await _manager.AddAsync(_mapper.Map<TModel>(item));
        return Ok(_mapper.Map<TViewModel>(addedEntity));
    }
    catch (CreateException)
    {
        return BadRequest();
    }
}

Thats my model: 那就是我的模型:

public class UserViewModel : IViewModel
{
    public Guid? Id { get; set; }
    public string SecurityStamp { get; set; }

    public Gender? Gender { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }

    public IEnumerable<string> Roles { get; set; }
}

And this is my request: 这是我的要求:

在此处输入图片说明

And this is my result: 这是我的结果:

在此处输入图片说明

You need to specify [FromBody] for the controller parameter so the framework can bind correctly to your model. 您需要为控制器参数指定[FromBody] ,以便框架可以正确绑定到模型。 Please see the documentation on Model Binding in ASP.NET Core . 请参阅ASP.NET Core中有关模型绑定的文档。

Try: 尝试:

[HttpPost]
public async Task<ActionResult<TViewModel>> Post([FromBody] TViewModel item)
{
    // your code
}

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

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