I have this code to append a file to a certain index in the collections sent from javascript to a C# endpoint.
const data = row.querySelector(`td[data-bs-column="Action"]`);
if (data) {
const uploadElement = data.querySelector("input[type='file']");
if (uploadElement && uploadElement.files.length > 0) {
const files = Array.from(uploadElement.files);
// formData.append(`vm.Items[${itemIndex}].Attachment`, file);
if (files && files.length > 0) {
files.forEach((file, index) => {
formData.append(`Items[${itemIndex}].Attachments`, file);
});
}
}
}
//ENDPOINT
[HttpPost, Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
public async Task<IActionResult> JSUpdateEvaluationItems(PerformanceViewerViewModel vm)
{
var files = HttpContext.Request.Form.Files; //TEST TO CHECK IF THE FILE IS RECEIVED
var result = await performanceRepository.UpdateEvaluationItems(vm);
return Json(result.ToString());
}
As you can see here, i have added this line of code var files = HttpContext.Request.Form.Files; //TEST TO CHECK IF THE FILE IS RECEIVED
to check if the formdata sent to the endpoint has a files. And it has the files received from the client.
Now, on the viewmodel on my endpoint is desiged like this
public class PerformanceViewerViewModel : BaseModel
{
public Credentials? Credentials { get; set; }
public Credentials? KRAOwner { get; set; }
public List<Entities.Users.Role>? Roles { get; set; }
public PerformanceConfiguration? Configuration { get; set; }
public PerformanceInformation? Information { get; set; }
public List<Category>? Categories { get; set; }
public List<Goal>? Goals { get; set; }
public List<Item>? Items { get; set; }
public List<Status>? Statuses { get; set; }
public List<CycleApprover>? Approvers { get; set; }
public List<SelectListItem>? StatusItems { get; set; }
public List<AppraisalItem>? AppraisalItems { get; set; }
public List<Unit>? Units { get; set; }
public List<Column>? Columns { get; set; }
}
The object that is receiving the the attachment is the List<Item>? Items
.
But when I check on the Attachments inside the Items, there is no file appended.
The model Item is designed like this
public class Item : BaseModel
{
public Guid ParentTempID { get; set; }
public int ItemID { get; set; }
public int GoalID { get; set; }
public string? ItemName { get; set; }
public decimal ItemPercentage { get; set; }
public int UnitID { get; set; }
public string? UnitName { get; set; }
public bool Ratio { get; set; }
public string? Consolidated { get; set; }
public string? Result { get; set; }
public decimal ItemWeight { get; set; }
public bool OverrideWeight { get; set; }
public int ActiveCommentID { get; set; }
public List<IFormFile>? Attachments { get; set; }
public List<CompanyTarget>? CompanyTargets { get; set; }
public List<string>? Filenames { get; set; }
}