2

I have a 2d array of strings (12x5) that I want to pass from a javascript function to an asp.net mvc controller action. Using developer tools in IE I know that the array is populated with what I want it to be, so the issue is in or around the post function.

var dateArray = new Array();
//Populate Data

$.post("/Event/SetDateCalculationParameters", { dates: dateArray }, function () {
    //Stuff
});
}

And here is the MVC controller action

public ActionResult SetDateCalculationParameters(string[][] dates)
    {
        //Do stuff

        return Json(true);
    }

In the controller action, there are 12 items in the dates array, but they are all null. Ive been at this for a couple hours and am stumped. Is there an easier way to do this? Or am I missing something?

3
  • how does it look in the request object? Just to get an idea on to how this could be handled? Commented Jul 26, 2012 at 6:21
  • Im not sure I follow. Are you asking for an example of the data in the js before it gets sent? Commented Jul 26, 2012 at 18:14
  • It's worth mentioning that this question is for passing jagged arrays, not 2d which would be defined as string[,]. Commented Jun 21, 2016 at 10:05

2 Answers 2

3

You could send them as a JSON request:

var dateArray = new Array();
dateArray[0] = [ 'foo', 'bar' ];
dateArray[1] = [ 'baz', 'bazinga' ];
// ... and so on

$.ajax({
    url: '@Url.Action("SetDateCalculationParameters", "Event")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify({ dates: dateArray }),
    success: function (result) {

    }
});

The action signature must look like this:

[HttpPost]
public ActionResult SetDateCalculationParameters(string[][] dates)
Sign up to request clarification or add additional context in comments.

2 Comments

Any time I tried to use an array in the action sig it would be null. When I used a straight up string it worked. So I just parsed it into the 2d array in the action's code.
I know I am a lot late here, but I had switched to using the ajax call instead of the post and got the result I needed. I have a feeling it may have to do with the contentType. I was also able to use List<string[]> parmName instead of string[][] parmName. Just allowed me too loop through the array using a foreach.
1

To solve the same problem I have created JsonModelBinder and JsonModelAttribute that should be applied to the parameter:

public class JsonModelBinder : IModelBinder
    {
        private readonly static JavaScriptSerializer _serializer = new JavaScriptSerializer();

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];

       if (string.IsNullOrEmpty(stringified))
            return null;

        return _serializer.Deserialize(stringified, bindingContext.ModelType);
    }
}

public class FromJsonAttribute : CustomModelBinderAttribute
{
    public override IModelBinder GetBinder()
    {
        return new JsonModelBinder();
    }
}

Your controller will look as follow:

public ActionResult SetDateCalculationParameters([FromJson]string[][] dates)

Also you should stringify your array:

$.post("/Event/SetDateCalculationParameters", { dates: JSON.stringify(dateArray)}, function () {             //Stuff         });         }

It works for me.

Comments

Your Answer

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