0

Originally, I was passing one parameter to my MVC Controller via Ajax and it was working correctly. The parameter data was being received without any problems. However, I now need to pass an extra parameter and when I do this, neither parameter is sending data to the Controller? Thanks!

Ajax Code:

        function uploadFile() {
        var fileData = new FormData($(form1)[0]); //THIS IS A FILE UPLOAD
        var details = JSON.stringify(markers); //MARKERS IS AN ARRAY

        $.ajax({
            url: '../Home/FilePost',
            type: 'Post',
            success: function (result) {
                var newWindow = window.open('LasView?fileName=' + result, "", "new window");

            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            },
            xhr: function () {  // Custom XMLHttpRequest
                var myXhr = $.ajaxSettings.xhr();
                if (myXhr.upload) { // Check if upload property exists
                    // TODO...
                }
                return myXhr;
            },
            error: function () { },
            data: { filePost: fileData, googleMarkers: details },
            cache: false,
            contentType: false,
            processData: false
        });
    }

My Controller:

[HttpPost]
public string LasPost(HttpPostedFileBase filePost, string googleMarkers){
return something;
}

My Array:

 var markerObject = {
     lat: marker.position.lat(),
     lng: marker.position.lng()
    };
     markers.push(markerObject);
8
  • You cannnot mix FormData and objects - you need to append the extra data to FormData Commented Apr 11, 2018 at 11:06
  • But it makes no sense to make the googleMarkers a string if its an array - what it the array? Commented Apr 11, 2018 at 11:07
  • It's an array of objects, so I just stringify it. What's wrong that? Is that a problem? Commented Apr 11, 2018 at 11:09
  • Why in the world would you convert it to a string and then have to convert it back again in the controller? Show what it is (just 1 or 2 items) so that an answer can be added) Commented Apr 11, 2018 at 11:11
  • Why, what's another way? I've added my object code. Commented Apr 11, 2018 at 11:17

1 Answer 1

1

You cannot mix FormData and objects. You must .append() each name/value pair to the FormData instance.

Because you stringified your array, and are binding to a string googleMarkers parameter, then your code would need to be

function uploadFile() {
    var fileData = new FormData($(form1)[0]); //THIS IS A FILE UPLOAD
    var details = JSON.stringify(markers); //MARKERS IS AN ARRAY

    fileData.append('googleMarkers', details); // append the name/value pair
    $.ajax({
        ....
        data: fileData, // send only the FormData instance
        ....
    });
})

However you should be taking advantage of MVC's model binding features and posting data which binds to a model representing your coordinates, for example

public class Coordinate
{
    public double Latitude { get; set; }
    public double Longitude { get; set; }
}

and the POST method would then be

[HttpPost]
public string LasPost(HttpPostedFileBase filePost, IEnumerable<Coordinate> googleMarkers)

and to send that data

var fileData = new FormData($(form1)[0]);
for(var i = 0; i < markers.length; i++)
{
    fileData.append('[' + i + '].Latitude', markers[i].lat);
    fileData.append('[' + i + '].Longitude', markers[i].lng);   
}
$.ajax({
    ....
    data: fileData,
    ....
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.