繁体   English   中英

Asp.net Web Api。 将复杂模型发布到Controller且收到的模型不正确

[英]Asp.net Web Api. Post a complex model to Controller and received model incorrect

我在将复杂模型对象从客户端发布到Web Api控制器时遇到问题。 我的模型结构是:

public class PaymentModel
{
    public Credit Crediter { get; set; }
}

public class Credit : ICredit
{
    public int BankInformationId { get; set; }
}

public interface ICredit
{
    int BankInformationId { get; set; }
}

public sealed class CrediterEmployee:Credit
{
    public int EnployeeId { get; set; }
}

我试图创建一个模型以发布到API控制器:

var param = new PaymentModel
{
    Crediter = new CrediterEmployee
    {
        BankInformationId = 4928,
        EnployeeId = 7013
    },
}

在API控制器中,我收到了一个模型对象,但是对于Crediter我无法转换为CreditEmployee 当我尝试投射时为null

如何将Crediter投给CreditEmployee

你尝试过这样的事情吗? ...并且您是否尝试过使用POSTMAN之类的工具进行POST? (确保您没有发送空的邮件)

[HttpPost]
[ResponseType(typeof(Credit))]
public async Task<IHttpActionResult> Post([FromBody] PaymentModel payment)
        {
            try
            {
                if (payment == null || payment.Crediter == null) return BadRequest();
                var response = new Credit
                {
                    BankInformationId = payment.Crediter.BankInformationId
                };

                return Ok(response);
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }
        }

暂无
暂无

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

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