I have the following jquery ajax:
$(".gridster ul").gridster({
widget_margins: [5, 5],
widget_base_dimensions: [280, 280],
draggable: {
stop: function(event, ui){
var orderState = {
photos: $(".gridster ul").data("gridster").serialize(),
promoid: $.getUrlVar('promoId')
};
$.ajax({
url: "/Photos/SetOrder",
data: {neworder:orderState},
dataType: 'json',
type: 'POST'
});
Which I send to a Monorail 2.1 controller.
The orderState object from JS looks likes this:
neworder[photos][0][height]=1
neworder[photos][0][id]=98315
neworder[photos][0][width]=1
neworder[photos][0][x]=2
neworder[photos][0][y]=1
neworder[photos][1][height]=1
neworder[photos][1][id]=98316
neworder[photos][1][width]=1
neworder[photos][1][x]=1
neworder[photos][1][y]=1
neworder[photos][2][height]=1
neworder[photos][2][id]=98317
neworder[photos][2][width]=1
neworder[photos][2][x]=1
neworder[photos][2][y]=2
neworder[photos][3][height]=1
neworder[photos][3][id]=98318
neworder[photos][3][width]=1
neworder[photos][3][x]=1
neworder[photos][3][y]=3
neworder[promoid]=163844
And it should map to the following c# viewmodel class:
public class OrderViewModel
{
public int height { get; set; }
public int width { get; set; }
public int x { get; set; }
public int y { get; set; }
public int id { get; set; }
}
public class NewPromoImageOrderViewModel
{
public NewPromoImageOrderViewModel()
{
Photos = new List<OrderViewModel>();
}
public int PromoId { get; set; }
public List<OrderViewModel> Photos { get; set; }
}
Used by the following action:
[return: JSONReturnBinder]
[AccessibleThrough(Verb.Post)]
public object SetOrder([JSONBinder("neworder")] NewPromoImageOrderViewModel neworder)
{
//bla
}
And the response it gives me is this:
Message: Error building method arguments. Last param analyzed was neworder with value ''
StackTrace: at Castle.MonoRail.Framework.SmartDispatcherController.BuildMethodArguments(ParameterInfo[] parameters, IRequest request, IDictionary
2 actionArgs) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\SmartDispatcherController.cs:line 329 at Castle.MonoRail.Framework.SmartDispatcherController.InvokeMethod(MethodInfo method, IRequest request, IDictionary
2 extraArgs) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\SmartDispatcherController.cs:line 80 at Castle.MonoRail.Framework.ActionMethodExecutorCompatible.Execute(IEngineContext engineContext, IController controller, IControllerContext context) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\ActionMethodExecutor.cs:line 79 at Castle.MonoRail.Framework.Controller.RunActionAndRenderView() in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Controller.cs:line 1639Inner exception: ArgumentNullException
Message: Value cannot be null. Parameter name: s
StackTrace: at System.IO.StringReader..ctor(String s) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type) at Castle.MonoRail.Framework.Services.NewtonsoftJSONSerializer.Deserialize(String jsonString, Type expectedType) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Services\NewtonsoftJSONSerializer.cs:line 100 at Castle.MonoRail.Framework.JSONBinderAttribute.Bind(IEngineContext context, IController controller, IControllerContext controllerContext, ParameterInfo parameterInfo) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Attributes\JSONBinderAttribute.cs:line 135 at Castle.MonoRail.Framework.SmartDispatcherController.BuildMethodArguments(ParameterInfo[] parameters, IRequest request, IDictionary`2 actionArgs) in C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\SmartDispatcherController.cs:line 288
I'm pretty sure my mappings and viewmodel class are correct. JQuery does send the expected json object, but Monorail seems to have problems converting it. Could this be a bug?
EDIT:Well, Monorail wasn't at fault at here... The things i put through the post WERENT A JSON after all. After using this library: https://code.google.com/p/jquery-json/, I managed to convert the object to JSON. I thought that by choosing the dataType:"json" would have been enough, but apparently it's not.
The datatype option in the $.ajax call refers to the data that is expected. What you need to do is this:
$.ajax({
url: "/Photos/SetOrder",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({neworder:orderState}),
type: 'POST'
});