1

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)
{

}
2
  • Are you sure your array is correctly set up: json.com/json-array Commented Sep 8, 2015 at 8:16
  • Yes. the json structure should be correct. I manage to debug and reach to that point of Process. i can get id and lastDateTime but currentIds is null. Commented Sep 8, 2015 at 8:22

2 Answers 2

2

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)
{

}
Sign up to request clarification or add additional context in comments.

7 Comments

Sorry, i actually had my ajax setup as dataType : json.
I managed to debug and reached the controller's process.. but int[] currentIds is null.
no error, but the currentIds is null. suppose to get a list of integer array. I am using MVC 5
I also use MVC5... My code is .....public ActionResult Process(int id,DateTime? lastDataTime,int[] currentIds) { }$.ajax({ type: "POST", url:"@Url.Action("Process", "Home")" , data: { "id": id, "lastDataTime": lastDateTime, "currentIds": [1, 2, 3, 4] }, dataType: "json", traditional: true, success: function(msg){alert(msg)}
i didn't set to traditional : true. what is this?
|
1

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.

1 Comment

Thx. found the culprit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.