Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
Passing an object and files to Web Api 2 Controller from ajax Illegal Invocation

I am trying to pass from Ajax a ViewModel object and some files to an aspnet web api 2 controller but throws out an error with TypeError: Illegal invocation, if i remove the extra files object this works fine.

                           $.ajax({
                                url: `/api/issues/${ViewModels.issueViewModel.id}`,
                                method: "PUT",
                                data: {dto: ViewModels.issueViewModel, emailAttach: attachFileData},
                                headers: {
                                    'Authorization': 'Bearer ' + token
                                }
                            })

issueViewModel is a key value pair object and emailAttach will be files attached to a FormData The webapi2 controller

 public IHttpActionResult CreateIssue(IssueDto dto, List<HttpFileCollectionBase> emailAttach)
    {
        //Some logic to handle objects passed
    }

The IssueDto looks like this

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; }

All this information are bound to the ViewModel and passed to the controller And emailAttach is a key value FormData generated from thise code

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

}

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Try changing the data type of dto and files in the IssueDto class to string.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda