Su Beng Keong
Su Beng Keong

Reputation: 1034

Converting Javascript JSON to .NET integer array

I was trying to pass json data to .net mvc controller. it seems like mvc automatically converted id and lastdatetime to correct format, but not int[] currentIds, anyone know why?

var jsonData = {
                "id": id,
                "lastDataTime": lastDateTime,
                "currentIds": [1, 2, 3, 4]
            };


public void Process(int id, DateTime lastDateTime, int[] currentIds)
{

}

Upvotes: 1

Views: 57

Answers (2)

JsonStatham
JsonStatham

Reputation: 10364

Simplify this problem and get the array working first, try this:

View

var myArray = ["1", "2", "3","4"];


$.ajax(
                {
                    type: "POST", 
                    url: '/ControllerName/ActionMethod/',
                    data: JSON.stringify(myArray),
                    cache: false,
                    //dataType: "html",
                    success: ///            
})

Controller

public ActionResult Index(List<String> currentIds)
{
   ///
}

Debug this and check the list is populated then introduce the other objects.

Upvotes: 1

temUser
temUser

Reputation: 142

Please try this:

$.ajax({
            type: "POST",
            url:"@Url.Action("Index", "Home")" ,
            data: {
                "id": id,
                "lastDataTime": lastDateTime,
                "currentIds": [1, 2, 3, 4]
            },
           dataType: "json",
           traditional: true,
           success: function(msg){
                       alert(msg)
                   }
    });


public ActionResult Index(int id, DateTime lastDateTime, int[] currentIds)
{

}

Upvotes: 2

Related Questions