2

I am developing an application using angular as frontend and API in .net core. My API model is as below:

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

And the Post class is as below

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

The same class I am having in angular as well. Here I have to upload image and other properties with tags as well.

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.

I have tried below

  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");

Non of them worked. The tags is passing as array when I debug and watch the Request.Form but it is not assigning to the object. The Tags count is always 0.

I can provide more info if needed. thanks in advance.

Iftikhar Ali Ansari
  • 1,650
  • 1
  • 17
  • 27

0 Answers0