Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda