Estoy tratando de pasar de Ajax un objeto ViewModel y algunos archivos a un controlador aspnet web api 2 pero arroja un error con TypeError: Invocación ilegal, si elimino el objeto de archivos adicionales, esto funciona bien.
$.ajax({ url: `/api/issues/${ViewModels.issueViewModel.id}`, method: "PUT", data: {dto: ViewModels.issueViewModel, emailAttach: attachFileData}, headers: { 'Authorization': 'Bearer ' + token } })
issueViewModel es un objeto de par de valores clave y emailAttach serán archivos adjuntos a un FormData El controlador webapi2
public IHttpActionResult CreateIssue(IssueDto dto, List<HttpFileCollectionBase> emailAttach) { //Some logic to handle objects passed }
El IssueDto se ve así
public int Id { get; set; } public string Description { get; set; } public string Note { get; set; } public List<int> PersonId { get; set; } //Extra public List<string> ApplicationName { get; set; } public List<string> AttachedFiles { get; set; } public List<PslApplicationDto> PslApplicationDto { get; set; } public UrgencyFlag UrgencyFlag { get; set; } public CompletionFlag CompletionFlag { get; set; } public DateTime? CompletionDate { get; set; } public DateTime? DateIssueRegistered { get; set; } public DateTime? LastUpdate { get; set; } public int CompletionDuration { get; set; } public SolutionDto SolutionDto { get; set; } public PsUserDto PsUser{ get; set; } public EmailDto EmailDto { get; set; } public IssueCategorieDto Category { get; set; } public List<PersonDto> PersonDtos { get; set; } public List<IssueTagsDto> IssueTags { get; set; } public List<int> IssueTagsId { get; set; } // Extra info public int CategorieId { get; set; } public int PsUserId { get; set; }
Toda esta información está vinculada a ViewModel y se pasa al controlador. Y emailAttach es un valor clave FormData generado a partir de este código.
function attachFile(e) { e.stopImmediatePropagation(); e.preventDefault(); var attachmentInput = document.getElementById("attachInput"); if (typeof (attachmentInput.files[0]) === "undefined") { return toastr.error("Please select a file to attach"); } attachFileData.append(attachmentInput.files[0].name, attachmentInput.files[0]); appendAttachmentToForm(attachmentInput.files[0]);
}
Intente cambiar el tipo de datos de dto y los archivos en la clase IssueDto a cadena.