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

218
Views
Ajax FormData return null

I created a form. I am sending the data in this form as JSON.

If I just submit the formData (I also change the controller to [FromBody] Image request) and set the contentType and processData to false, the formData is not null.

I think "document" returns null because I am sending the data as JSON. How can I solve this issue I'm stuck in this issue.

Ajax Request

   let myProfile = {
        id: 0,
        title: "",
        text: "",
        document: File,
    };
    
        myProfile.title = "Lorem ipsum"; 
        myProfile.text = "Lorem ipsum"; 

        var formData = new FormData();
        formData.append('file', $('#file').get(0).files[0]);
        formData.append('fileName', $("#fileName").val());

        var myFile = formData.get('file');
        myProfile.document = myFile;

    $.ajax({
       url: `/admin/myprofile`,
       type: "POST",
       data: JSON.stringify(myProfile),
       contentType: "application/json",
       dataType: 'json',
       success: ....

Cshtml

    <div class="form-group">
        <input type="file" name="document" asp-for="document" id="file" />
    </div>

Controller

    [HttpPost("myprofile")]
    public IActionResult MyProfile([FromBody] MyProfileDTO request)
    {
        return ...
    }

Class

    public class Image
    {
      public IFormFile file { get; set; }
      public string fileName { get; set; }
    }

    public class MyProfileDTO 
    {
        public string Title { get; set; }
        public string Text { get; set; }
        public Image Document{ get; set; }
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ajax Request

   let myProfile = {
        id: 0,
        title: "",
        text: "",
        document: File,
    };
    
        myProfile.title = "Lorem ipsum"; 
        myProfile.text = "Lorem ipsum"; 

        var formData = new FormData();
        formData.append('file', $('#file').get(0).files[0]);
        formData.append('title', myProfile.title);
        formData.append('text', myProfile.text);


    $.ajax({
       url: `/admin/myprofile`,
       type: "POST",
       data: formData ,
       contentType: "application/json",
       success: ....

Controller

    [HttpPost("myprofile")]
    public IActionResult MyProfile([FromBody] MyProfileDTO request, IFormFile file)
    {
        return ...
    }

Class

    

    public class MyProfileDTO 
    {
        public string Title { get; set; }
        public string Text { get; set; }
    }
about 4 years ago · Juan Pablo Isaza 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!