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:
Make sure that
viewModel.get("entity.TemplateFilesData")
is returning a valid list ofAttachmentDTO
objects. You can check this by adding a debug statement to print out the returned list.Make sure that you are correctly appending the
ID
andFileName
properties to the options object. You should use theappend()
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);
Make sure that you are correctly accessing the
ID
andFileName
properties of eachAttachmentDTO
object. Make sure that these properties have values and are notnull
or undefined.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.