I am trying to post file along with other form data to MVC controller using jquery Ajax.
jQuery Ajax call
function SaveStundent () {
var formData = new FormData();
var file = document.getElementById("studImg").files[0];
formData.append("studImg", file);
var studentDetails = {
Name: $('#txtName').val().trim()
};
formData.append("studentDetails", studentDetails);
$.ajax({
type: "POST",
url: "@(Url.Action("CreateStudent", "Student"))",
data: formData,
processData: false,
contentType: false,
success: function (response) {
}
});
}
MVC Controller
[HttpPost]
public ActionResult CreateStudent(Student studentDetails)
{
// ...
}
Student Model
public class Student
{
public string Name { get; set; }
}
Though I was able to get the file in the Request, the studentDetails parameter is always null in MVC controller.
Studentmodel and input field you are using for the file upload in the form. please!