Click here to Skip to main content
14,921,505 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am trying to pass a JS array to a MVC Controller. By using the same code i was able to do the same for API Controller. But i am not able to do the same using MVC Controller.

Can anyone please help

This is the JS side
JavaScript
function GetColumnmappingValue() {
    var columnMappings = [];
    var count = 0;
    $('.row').each(function () {
        if (count > 0) {
            var sourceColumn = "";
            var destinationColumn = "";
            var sourceColumnIndex = "";

            $(this).find(".column").each(function () {

                var input = $(this).find("select").each(function (i) {
                    if (sourceColumn == "") {
                        sourceColumn = $(this).find("option:selected").text();
                        sourceColumnIndex = $(this).find("option:selected").val();
                    }
                    else {
                        destinationColumn = $(this).find("option:selected").text();
                    }
                });
            });

            //if (sourceColumn.toLowerCase() != "select" && destinationColumn.toLowerCase() != "select")
            columnMappings.push({ SourceColumnIndex: sourceColumnIndex, SourceColumn: sourceColumn, DestinationColumn: destinationColumn });
        }
        count++;
    });


    $.ajax({
        type: "POST",
        url: "/ExcelImport/SaveMappings",
        data: { '': columnMappings },
        dataType: "json",
        success: function () { alert("Mapping Successful") },
        failure: function () { alert("not working..."); }
    });

}


and this is the controller

C#
[HttpPost]
        public IEnumerable<ColumnMapping> SaveMappings(IEnumerable<ColumnMapping> columnMappings)
        {
            // do some registration work
            CreateXML(columnMappings);
            return columnMappings;
        }


The problem is that i am getting the array count , but all values inside it are null ..

Thanks
Arjun Menon
Posted

You're seeing those results because MVC uses Newtonsoft to deserialize. It's aware than an array is there, but it doesn't know how to assign values because the object key is being mapped to null.

This should work fine if you fix the JSON:

JavaScript
$.ajax({
           type: "POST",
           url: "/ExcelImport/SaveMappings",
           data: { columnMappings: columnMappings },
           dataType: "json",
           success: function () { alert("Mapping Successful") },
           failure: function () { alert("not working..."); }
       });


This works in my jquery powered MVC implementations.
   
JavaScript
var Mapping= new Object();
Mapping.Column1= "EmployeeID";
Mapping.Column2= "Name";
Mapping.Column3= "Salary";


var param = new Object();
paramArray.columnMappings= new Array();
paramArray.columnMappings.push(Mapping);

$.ajax({
        type: "POST",
        url: "/ExcelImport/SaveMappings",
        data: JSON.stringify(paramArray),
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function () { alert("Mapping Successful") },
        failure: function () { alert("not working..."); }
});
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900