Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

0

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!