简体   繁体   English

参数数据Null Ajax到MVC控制器

[英]Parameter Data Null Ajax to MVC Controller

Originally, I was passing one parameter to my MVC Controller via Ajax and it was working correctly. 最初,我是通过Ajax将一个参数传递给我的MVC Controller,并且它可以正常工作。 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: Ajax代码:

        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. 您不能混合使用FormData和对象。 You must .append() each name/value pair to the FormData instance. 您必须将每个名称/值对.append()FormData实例。

Because you stringified your array, and are binding to a string googleMarkers parameter, then your code would need to be 由于您已对数组进行了string googleMarkers化,并且绑定到string googleMarkers参数,因此您的代码需要

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 但是,您应该利用MVC的模型绑定功能并发布绑定到表示您的坐标的模型的数据,例如

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

and the POST method would then be 然后POST方法将是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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