0

I Want to send formData to api controller via jquery ajax. This is My code in view:

$("#btnReject").click(function() {
  UploadData = new FormData();
  UploadData.append('UserName',$("#txtUserName").val());
  // HTML file input, chosen by user
  UploadData.append("userfile", fileInputElement.files[0]);
  $.ajax({
     url: 'Api/DynamicForm/Reject',
     dataType: "json",
     type: "post,
     contentType: false,
     processData: false,
     data: UploadData
  });
}

and my controller code:

public class DynamicFormController : ApiController
{
    [System.Web.Http.HttpPost]
    public IHttpActionResult Reject (DynamicFormDto model)
    {
       //some code
        return Json(new isValid = true});
    }
}

In DynamicFormDto:

Public class DynamicFormDto
{
   public string UserName {get; set;}
   public string Model {get; set;}
}

I encountered with this error in client side: 415 : Unsupported Media Type

when I remove "contentType: false" error fixed but in action "model" is null.

What should I do?

2
  • The ModelBinder has issues with binding multipart requests to complex types (in that it doesn't work). See the duplicate I marked for more information. Also note that you could instead write your own custom model binder instead, see this answr for how to achieve that Commented Apr 4, 2018 at 8:20
  • @RoryMcCrossan, Did you pick the wrong dupe target? (that has nothing to do with OP's question - they are already using FormData) Commented Apr 4, 2018 at 9:22

1 Answer 1

0
 public class DynamicFormController : ApiController
{
    [System.Web.Http.HttpPost]
    public IHttpActionResult Reject ()
    {

      var name = HttpContext.Current.Request["UserName"];
      var httpPostedFile = HttpContext.Current.Request.Files["userfile"];
    //some code

        return Json(new isValid = true});
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

A bit of explanation would be awesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.