-1

I have a array and I get data from the array and going to pass it to the controller through ajax call. But the problem is it hits the controller side with all the null values.(Data not passes,null passes )

Client Side Code

for (var j = 0; j < NewsGlobalArray.length; j++) {  
    var NewsRequestModel = {
        DESCRIPTION: NewsGlobalArray[j]['DESCRIPTION'] // news description comes here.i checked it with console.log
     }}

    $.ajax({
        url: $('#addNewsRequest').val(),
        type: "POST",
        data: { newsRequest: NewsRequestModel },
        dataType: "json",
        success: function (referenceNo) {
            //success
        }
    });
}

My Controller

[HttpPost]
public JsonResult AddNewsRequest(NewsRequestModel newsRequest) // hits here with null values
{
    //Some coding goes here.
}

My Model

public class NewsRequestModel 
{
    public int NEWSREQUESTID { get; set; }
    public string DESCRIPTION { get; set; }
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41
TechGuy
  • 4,298
  • 15
  • 56
  • 87

2 Answers2

0

Try this: just add traditional:true in ajax call

 $.ajax({
        type: "POST",
        url: $('#addNewsRequest').val(),
        data: JSON.Stringify({ newsRequest: NewsRequestModel }),
        dataType: "json",
        traditional:true,
        success: function (res) {
           //do something
        }
    });
Mohammad Imran
  • 125
  • 1
  • 6
  • @TechGuy Please refer following question http://stackoverflow.com/questions/6327019/how-do-i-pass-this-js-array-to-my-mvc-3-controller/37072244#37072244 – Mohammad Imran May 13 '16 at 07:26
  • `traditional` is for sending arrays, but OP is only sending one object. –  May 13 '16 at 08:16
0

I think you need to this:

var myObject = new Object();
myObject.name = "John";
myObject.age = 12;

then pass myObject to ajax call and get on controller by name.

SᴇM
  • 7,024
  • 3
  • 24
  • 41
Akram Khan
  • 114
  • 8