7

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.

Priyanka
  • 169
  • 3
  • 10
Gopi
  • 5,656
  • 22
  • 80
  • 146
  • How are you invoking the form submission? using submit button of the form or any other button outside the form? – TanvirArjel Jan 28 '19 at 05:44
  • Have u add Http.Post in action? – BeiBei ZHU Jan 28 '19 at 05:45
  • @TanvirArjel The jQuery method 'SaveStundent' will be called on 'Submit' button onclick event. – Gopi Jan 28 '19 at 05:45
  • @beibeizhu yes I did. Sorry I missed to add the code – Gopi Jan 28 '19 at 05:46
  • @Gopi Add your full `Student` model and input field you are using for the file upload in the form. please! – TanvirArjel Jan 28 '19 at 05:46
  • Sounds like your viewmodel passed as null because it doesn't match with the definition. Check these posts since you're passing with FormData object: https://stackoverflow.com/questions/20629105/ajax-fileupload-jquery-formdata-in-asp-net-mvc & https://stackoverflow.com/Questions/581703/how-to-do-a-asp-net-mvc-ajax-form-post-with-multipart-form-data. – Tetsuya Yamamoto Jan 28 '19 at 05:46
  • @Gopi: try to change your post data like : data: {studentDetails=studentDetails} – BeiBei ZHU Jan 28 '19 at 05:50

1 Answers1

9

try this

function SaveStundent () {
    var formData = new FormData();

    var file = document.getElementById("studImg").files[0];
    formData.append("studImg", file);

    var Name= $('#txtName').val().trim()

    formData.append("Name", Name);

        $.ajax({
        type: "POST",
        url: "@(Url.Action("CreateStudent", "Student"))",          
        data: formData,
        processData: false,
        contentType: false,
            success: function (response) {
            }
        });
}

then change your action to get image as follow

[HttpPost]
public ActionResult CreateStudent(Student studentDetails,HttpPostedFileBase studImg)
{
// ...
}

Working Code

<form id="frm" enctype="multipart/form-data">
    <input type="file" name="studImg" id="studImg" />
    <input type="text" name="txtName" id="txtName" />
    <input type="button" value="Save" onclick="SaveStundent()" />
</form>

<script>
    function SaveStundent () {
        var formData = new FormData();
        var file = document.getElementById("studImg").files[0];
        formData.append("studImg", file);

        var Name = $('#txtName').val().trim()
        formData.append("Name", Name);

        $.ajax({
        type: "POST",
        url: "@(Url.Action("CreateStudent", "Student"))",          
        data: formData,
        processData: false,
        contentType: false,
        success: function (response) {
            }
        });
    }
</script>

public ActionResult CreateStudent(string Name, HttpPostedFileBase studImg)
{
    return null;
}
Gopi
  • 5,656
  • 22
  • 80
  • 146
Hamzeh.Ebrahimi
  • 305
  • 1
  • 6