[RESOLVED]How to pass formData to controller with model using jQuery ajax in MVC

I have formData which contains files(not in the form) and form model object, Now I want pass model object to controller with formData, I have tried in different ways but not getting..

var data = formData; // Which contains files which are not in the form.

var model = $("#formAddEmployees").serializeArray();

$.ajax({
type: "POST",
url: ‘/Employer/AddEmployees?model=’+model,
data:data,
success: function (data) {
alert("success");
},
error: function (error) {
alert("Error");
}
});

public ActionResult AddEmployees(ModelClass model)
{

if (Request.Files.Count > 0) //Not Getting
{
}

return Json("",JsonRequestBehaviour.AllowGet);
}

I have tried like above, but formData is not Getting..!! please help anyone..

Since you are posting to the AddEmployees Action within your Employer method, you need to ensure that your AddEmployees method is properly named and decorated with the verb (POST) that you intend to use :

[HttpPost]
public ActionResult AddEmployees(ModelClass model)
{
      // Your code here
}
File are sent separate from form data. They are included in the post data as base64 mime encoded. $.ajax does it support posting files. You need to use an iframe or the new webapi support int html 5 browsers.

There are several libraries to do this. Google ajax file post.

I have name properly, it is my mistake to mention correct question format, but is this possible to send formdata with model object??

Well brother you cannot send files and your model data together.  

there is only one way  use a jquery file upload save your file first store its path in a session object and use it when sending data to your controller action. 

Leave a Reply