如何将2个对象发送到我的API(ASP.NET)?

卢卡斯M

我有一个我无法解决的问题。我想将用户创建的卡片组传输到我的API,但问题是卡和卡片组是数据库中的两个不同实体,这就是为什么我需要将卡片组和列表卡的信息传递给API的原因将它们添加到我的数据库。

我的实体:

    public class Card
        {
    
            public Card() {
                this.Decks = new HashSet<Deck>();
            }
    
            public int Id { get; set; }
    
            [Column(TypeName = "json")]
            public string Content { get; set; }
    
            [NotMapped]
            public virtual ICollection<Deck> Decks { get; set; }
        }

public class Deck
    {

        public Deck() {
            this.Cards = new HashSet<Card>();
        }

        public int Id { get; set; }
        public string Name { get; set; }

        [DataType(DataType.Date)]
        public DateTime CreateAt { get; set; }

        public User User { get; set; }
        [NotMapped]
        public virtual ICollection<Card> Cards { get; set; }
    }

public class Join
    {
        public int DeckId { get; set; }
        public Deck Deck { get; set; }
        public int CardId { get; set; }
        public Card Card { get; set; }
    }

我的API:

[HttpPost]
public void Add ([FromBody] JsonObject request) {
            
}

JSON:

{ 
    "Deck":{
        "Name": "", 
        "CreateAt": "2007-07-15", 
        "User": "null"
    },
    "Crads":[
        {"content": {}}, 
        {"content": {}} 
    ]
}

响应:

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "|99e854f-4f63859a690203b6.",
    "errors": {
        "$.Deck.User": [
            "The JSON value could not be converted to MTG_Deck.Models.User. Path: $.Deck.User | LineNumber: 4 | BytePositionInLine: 22."
        ]
    }
}
迭戈·林孔

我认为最好在您的API中收到一个DTO,该DTO封装了您需要的两个类(Deck和Card)。然后在方法内部获取所需的信息,并将实体正确保存在数据库中。

public class AddDeckAndCardDTO
{
    public Deck Deck { get; set; }
    public List<Card> Card { get; set; }
}

并在API中添加方法

[HttpPost]
public void Add ([FromBody] AddDeckAndCardDTO request) 
 {
     var card = request.Card;
     var deck = request.Deck
        
     // the code to mapped entities
     // the code to save your entities
 }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将JS对象的集合发送到ASP.NET API服务?

如何将GET请求从Internet Explorer发送到Asp.Net Core API?

如何将XML从一个ASP.NET页发送到另一页?

如何将多部分/表单数据发送到ASP.NET Core Web API?

如何将表单的数据发送到用Angular中的[FromForm]装饰的ASP.NET Core Web API操作?

ASP.NET Web API将复杂对象发送到前端

如何将带有 FormData 的地图对象发送到 .NET API?

如何将多个值从Asp.Net WebApi发送到Angularjs控制器?

如何将简单的值从axios.post发送到asp.net core?

如何将数据从JQuery Ajax发送到ASP.NET页?

如何将文件发送到列表-.NET Core Web API-邮递员

如何将图像文件从Angular发送到.net核心API

将 ViewModel 发送到 API ASP.net Core 2

如何使用angular将文件发送到asp.net Web API?

如何使用 fetch 将 FormData 从 javascript 发送到 ASP.NET Core 2.1 Web API

如何将选定的列表框值从一个列表框发送到ASP.NET MVC中的另一个

如何将数据从使用 javascript 和 ASP.NET 生成的字段发送到 Microsoft SQL?

ASP.NET表单-如何将表单数据发送到外部WCF服务

如何将包含excel文件的表单数据发送到asp.net core中的控制器

我如何在不使用会话的情况下将对象从Asp.net MVC控制器发送到Asp.Net WebForm?

ASP.NET MVC 将 List<Foo> 集合从我的视图发送到控制器

如何将命令发送到 VB.NET 中的 CMD 窗口

如何将发布数据从React前端发送到.NET后端

将图像从 ASP.NET Web API 发送到 Angular 客户端

将值从 Windows 窗体应用程序发送到 Asp.Net Core Api

将 ASP.NET MVC HttpContext 发送到 Web Api HttpContext

如何使用axios.put将JSON发送到ASP.NET控制器

如何通过ActionLink将项目发送到ASP.NET MVC控制器?

如何使用Signalr将消息发送到ASP.NET Core中的特定组?