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?

share|improve this question
how does it look in the request object? Just to get an idea on to how this could be handled? – SteenT Jul 26 '12 at 6:21
Im not sure I follow. Are you asking for an example of the data in the js before it gets sent? – John S Jul 26 '12 at 18:14
feedback

2 Answers

up vote 2 down vote accepted

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)
share|improve this answer
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. – John S Jul 27 '12 at 17:54
feedback

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.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.