• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

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

}

almost 3 years ago · Santiago Trujillo
1 answers
Answer question

0

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

almost 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error