Previously, I just had a requirement to create a form that contains only data to be sent to the action and not the image, now, I've confronted with a requirement to POST the data along with an image to the action. I've no idea how to append the image to the data so that I can get it work to proceed further in action.
This is how I'm posting the data and the image together but I receive null
in action :
HTML
<input type="file" id="profilePic" name="file" value="Browse">
<input type="text" id="EmployeeCode" name="EmployeeCode" value="EmployeeCode">
<input type="text" id="SystemCode" name="SystemCode" value="SystemCode">
<input type="button" value="Create" onclick="SaveEmpData()" />
I know how to get the input type text data and send it through ajax request and in a json format and save it to database, but when it comes to get the data and image through the ajax request in a json format, I've got no clue about how to do this.
JS
var empdata = { "SystemCode": "", "EmployeeCode": "" };
empdata.EmployeeCode = $("#EmployeeCode").val();
empdata.SystemCode = $("#SystemCode").val();
var data = new FormData();
var files = $("#profilePic").get(0).files;
data.append("Image", files[0]);
Ajax
$.ajax({
url: '/EmployeeMasterA/Create',
headers: headers,
data: JSON.stringify({ employeeMasterA: empdata, profilePic: data }),
type: 'POST',
dataType: 'json',
contentType: 'application/json',
traditional: true,
success: function (result) {
if (result.Success == "1") {
window.location.href = "/EmployeeMasterA/Index";
}
else {
alert(result.ex);
}
}
});
and Create
Action :
[HttpPost]
public ActionResult Create([Bind(Include = "SystemCode,EmployeeCode")] EmployeeMasterA employeeMasterA, HttpPostedFileBase profilePic)
{
var pic = System.Web.HttpContext.Current.Request.Files["Image"];
}
In the above code, the pic
variable is null
. My question is:
What is the proper way to get the data + image from the form, and send it through the ajax request to the action? Any help is deeply appreciated :)
Update
After referring to liran's answer, I tried binding by editing my code this way :
JS
var empbind = { "profilePic" : "", "employeeMasterA" : "" };
// Created empdata and data (image) object as mentioned above
empbind.employeeMasterA = empdata;
empbind.profilePic = data;
var empbindjson = JSON.stringify(empbind);
and changed the Ajax data
parameter as below :
$.ajax({
.
.
data: empbindjson,
.
.
});
And Changed my controller action :
[HttpPost]
public ActionResult Create([Bind(Include = "employeeMasterA,profilePic")] EmployeeBind bind)
{
var pic = System.Web.HttpContext.Current.Request.Files["Image"];
}
Both employeeMasterA,profilePic
are received null
in debug mode of the above code, this is my new class for binding :
public class EmployeeBind
{
public EmployeeMasterA employeeMasterA { get; set; }
public HttpPostedFileBase profilePic { get; set; }
}
Ok, Now, I've changed my code to bind it with the profilePic
, Is there any thing I doing wrong? because both bind
parameter of the create action
contains null for both employeeMasterA and profilePic.