简体   繁体   中英

Parameter Data Null Ajax to MVC Controller

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);

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,
    ....
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM