Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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? – Steen Tøttrup 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

2 Answers 2

up vote 3 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
    
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. – puddinman13 Jun 26 '13 at 15:59

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

Your Answer

 
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.