Skip to content Skip to sidebar Skip to footer

Http Post Request To C# Controller

I'm trying to make HTTP POST request to my C# controller, but I need to send in data an array, so I tried with JSON.stringify but when I start debugging, the input parameter in my

Solution 1:

Model binding has no idea what "MyModuleList" is. You can use a strongly typed model here and MVC will bind the JSON to it.

Consider JSON:

vardata = {
    moduleList: [
        { id:100, description:"some text"}
    ];
};

and models:

publicclassModuleListModel
{
    public List<ModuleModel> ModuleList { get; set; }
}
publicclassModuleModel
{
    publicint Id { get; set; }
    publicstring Description { get; set; }
}

and action:

[HttpPost]
public JsonResult Weather_post(ModuleListModel model)
{ 
    ... 
}

Combine that with @Timothy Shields' answer:

You're missing processData: false in your ajax call. Without that, ajax is going to try to pack the data into the URL query string. (See here: http://api.jquery.com/jQuery.ajax/)

and you should be good.

Solution 2:

You're missing processData: false in your ajax call. Without that, ajax is going to try to pack the data into the URL query string. (See here: http://api.jquery.com/jQuery.ajax/)

If data is { 'animal': 'elephant', 'count': 5 } and processData is true (the default), ajax will do the POST to the URL /Weather1/Weather_post?animal=elephant&count=5 with an empty HTTP request body. This is why you want processData: false.

Solution 3:

Try as follows:

var myData = { id:100, description:"some text"};
 var myDataArray= newArray();   
 myDataArray.push(myData); 
 var param = JSON.stringify(myDataArray);  
 $.ajax({
     dataType: "json",
     type: "POST",
     url: "/Weather1/Weather_post",
     contentType: "application/json; charset=utf-8",
     data: {'MyModuleList': param },
     success: function (data) {
          console.log(("OK"));
     },
     error: function (error)
     { console.log("NOT OK"); }
 })

Solution 4:

You may need to pass the parameter name with the data. Something like:

data: {'MyModuleList': JSON.stringify(myDataArray)},

See if this works for you.

Post a Comment for "Http Post Request To C# Controller"