Home > Blockchain >  Trying to pass data (a list) from client to server using FormData
Trying to pass data (a list) from client to server using FormData

Time:12-21

i have this class and the list that i want to populate

 public class ContentTemplateViewModel
    {
       public List<AttachmentDTO> TemplateFilesData { get; set; }
}

all the properties are inside this class

public class AttachmentDTO : AttachmentInfoDTO
    {
        public long ID { get; set; }
        public string Description { get; set; }
        public string Comments { get; set; }
        public string FileName { get; set; }
        public int FileSize { get; set; }
}

Im trying to bring to my controller the ID and FileName

using this code

    var options = new FormData();
    let existingFiles = viewModel.get("entity.TemplateFilesData"); // holding the items
                    existingFiles.forEach((item, index) => {
                        if (item) {
                            options.append('TemplateFilesData['   index   ']', item.FileName);
                            options.append('TemplateFilesData['   index   ']', item.ID);
                        }
                    });

for some reason i get [0] : null , [1] :null when i get the back to controller

CodePudding user response:

There are a few things you can try to fix this issue:

  1. Make sure that viewModel.get("entity.TemplateFilesData") is returning a valid list of AttachmentDTO objects. You can check this by adding a debug statement to print out the returned list.

  2. Make sure that you are correctly appending the ID and FileName properties to the options object. You should use the append() method to add each property as a separate entry in the options object, like this:

    options.append('TemplateFilesData['   index   '].ID', item.ID);
    options.append('TemplateFilesData['   index   '].FileName', item.FileName);
    
  3. Make sure that you are correctly accessing the ID and FileName properties of each AttachmentDTO object. Make sure that these properties have values and are not null or undefined.

  4. Make sure that you are correctly handling the index variable in the loop. Make sure that you are using the correct syntax for accessing the elements of the existingFiles list, and that you are not attempting to access an index that is out of bounds.

  • Related