2

我正在开发一个使用 Angular 作为前端和 .net 核心中的 API 的应用程序。我的 API 模型如下:

class Tag
    {
        public int ID {get;set;}
        public string Name {get;set;}
        public string Description {get;set;}
    }

Post类如下

class Post
    {
        public int ID {get;set;}
        ..... //other properties
        public IEnumerable<Tag> Tags {get;set;}
    }

我在角度上也有同样的课程。在这里,我还必须上传带有标签的图像和其他属性。

const formData = new FormData();
  if(blogRequest.imageContent){
    formData.append('file', postRequest.imageContent, postRequest.imageContent.name);
  }
  formData.append("Title",postRequest.title);
  .... // appended other data as well
  ///formData.append("Tags",tags); <-- how do I append tag object here. JSON.Stringify(tags) is not working as it is not assigning in the api model. I have to manually deserialize to object which I don't want.

我在下面尝试过

  var tags:Tag[]=[];
  var t1 = new Tag();
  t1.Name="some";
  t1.ID=0;
  t1.Description="desc";
  var t2 = new Tag();
  t2.Name="some";
  t2.ID=0;
  t2.Description="desc";

  tags[0] =t1;
  tags[1] = t2;


  formData.append("Tags[]","{Name:some}");
  formData.append("Tags[]","{Name:something}");

  formData.append("Tags[].Name","some");
  formData.append("Tags[].Name","something");

他们都没有工作。当我调试和观察标签时,标签作为数组传递,Request.Form但它没有分配给对象。标签计数始终为 0。

如果需要,我可以提供更多信息。提前致谢。

4

0 回答 0