简体   繁体   English

.NET Core 3 API Model 结合复合体类型与 ZC31C335EF37283C451B18BA0DD317

[英].NET Core 3 API Model Binding Complex Type with Angular 10

I have an Angular 10 application that calls a .NET Core 3 API.我有一个 Angular 10 应用程序,它调用 .NET Core 3 API。 I have a complex type that I am trying to model bind on a POST but running into an issue.我有一个复杂的类型,我试图在 POST 上绑定 model 但遇到问题。 On the Angular side, I have a type with an array and on the .NET side, the type I'm trying to bind to includes a List<>.在 Angular 端,我有一个带有数组的类型,在 .NET 端,我试图绑定的类型包括一个 List<>。 I'm trying to get the array to model bind with the List<>.我正在尝试将数组与 List<> 绑定到 model。 The array values are correct on the POST but I hit the error in the controller on the .NET side, specifically with the parameter when trying to bind. POST 上的数组值是正确的,但我在 .NET 一侧的 controller 中遇到错误,特别是在尝试绑定时使用参数。 Below is the error I'm receiving along with my code.以下是我收到的错误以及我的代码。

Error....错误....

" Bad Request status: 400" “错误请求状态:400”

"The JSON value could not be converted to App.Models.VideoCategoryModel[ ]" “JSON 值无法转换为 App.Models.VideoCategoryModel[]”

Angular Types... Angular型...

export class Video {
    constructor(public id?: number,
        public title?: string,
        public date?: string,
        public description?: string,
        public url?: string,
        public enabled?: boolean,
        public categories?: VideoCategory[]){ }
}

export class VideoCategory {
    constructor(public id?: number,
        public name?: string){ }
}

Here is the JSON being sent to the API这是发送到 API 的 JSON

{
    "title":"Test Title",
    "date":"2020-12-09",
    "description":"Test Description",
    "url":"C:\\fakepath\\Capture2.JPG",
    "categories":{
        "id":101,
        "name":"Platform Tutorials"
    }
}

.NET Core Controller Code.... .NET 核心 Controller 代码....

Models...楷模...

public class VideoModel
{
    public VideoModel()
    {
        Categories = new List<VideoCategoryModel>();
    }

    public int Id { get; set; }
    public DateTime Date { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public string VideoUrl { get; set; }
    public bool Enabled { get; set; }
    public List<VideoCategoryModel> Categories { get; set; }
}

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

Controller... Controller...

    [HttpPost("addvideo")]
    public IActionResult AddVideo([FromBody] VideoModel model)
    {
        try
        {
            //var id = _service.AddVideo(_mapper.Map<VideoModel, TaVideo>(model));
            //return Ok(id);

            return Ok();
        }
        catch(Exception ex)
        {
            return BadRequest($"Failed to add the new video: {ex}");
        }
    }

Any suggestions would be appreciated.任何建议,将不胜感激。 I'm obviously missing something or need to refactor the way I'm binding but I don't know the best way to do it.我显然遗漏了一些东西或需要重构我绑定的方式,但我不知道最好的方法。 Thanks in advance!提前致谢!

"The JSON value could not be converted to App.Models.VideoCategoryModel[ ]" “JSON 值无法转换为 App.Models.VideoCategoryModel[]”

Based on your VideoModel class, we can find that Categories property should be a collection of VideoCategoryModel object.根据您的VideoModel class,我们可以发现Categories属性应该是VideoCategoryModel object 的集合。 As @MohsinMehmood mentioned in comment, you should provide an array for categories field on your Angular frontend code.正如评论中提到的@MohsinMehmood,您应该在 Angular 前端代码上为类别字段提供一个数组。

"categories":[{
    "id":101,
    "name":"Platform Tutorials"
}] 

Besides, please note that you are passing file path in url field, but the property is named as VideoUrl in model class, to make model binding work well, you can try to specify the property name that is present in the JSON when serializing and deserializing with following code snippet. Besides, please note that you are passing file path in url field, but the property is named as VideoUrl in model class, to make model binding work well, you can try to specify the property name that is present in the JSON when serializing and deserializing带有以下代码片段。

[JsonPropertyName("Url")]
public string VideoUrl { get; set; }

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

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