Click here to Skip to main content
15,905,563 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
Updated 28-Mar-23 17:31pm

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.
 
Share this answer
 
Comments
Member 13677957 21-Aug-23 3:38am    
cool, that's worked like a charm
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..."); }
});
 
Share this answer
 
v2
can this work i want to send list data from mvc view to controler for insert if any sugestaion plz replay




function save(CART_NO, CATRIDGE) {
alert('hi');
var daten = {
CART_NO: 100,
CATRIDGE: 'Aman'
}
$.ajax({
type: 'post',
url: '@Url.Action("saveDetails", "Home")',
data: JSON.stringify(daten),
dataType: "json",
contentType: 'application/json',
success: function (response) {
alert("succese");
},
failure: function (response) {
},
error: function (response) {
},
});
}





[HttpPost]
public JsonResult saveDetails(Catridges_Model Catridges_Model)
{
var Result = service.saveDetailsCheckWise(Catridges_Model);
return Json(Result, JsonRequestBehavior.AllowGet);

}
 
Share this answer
 

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