我有以下 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'
});
我将其发送到 Monorail 2.1 控制器。
JS 中的 orderState 对象如下所示:
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
它应该映射到以下 c# viewmodel 类:
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; }
}
由以下操作使用:
[return: JSONReturnBinder]
[AccessibleThrough(Verb.Post)]
public object SetOrder([JSONBinder("neworder")] NewPromoImageOrderViewModel neworder)
{
//bla
}
它给我的回应是这样的:
消息:错误构建方法参数。分析的最后一个参数是 neworder,值为 ''
StackTrace:在 Castle.MonoRail.Framework.SmartDispatcherController.BuildMethodArguments(ParameterInfo[] 参数,IRequest 请求,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)在 C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\SmartDispatcherController.cs:Castle.MonoRail.Framework.ActionMethodExecutorCompatible 的第 80 行。在 C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\ActionMethodExecutor.cs:line 79 中执行(IEngineContext engineContext,IController 控制器,IControllerContext 上下文) Castle.MonoRail.Framework.Controller.RunActionAndRenderView() 在 C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Controller.cs:line 1639内部异常:ArgumentNullException
消息:值不能为空。参数名称:s
StackTrace:在 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) 在 C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Services \NewtonsoftJSONSerializer.cs:C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail- 中 Castle.MonoRail.Framework.JSONBinderAttribute.Bind(IEngineContext context, IController controller, IControllerContext controllerContext, ParameterInfo parameterInfo) 的第 100 行98c93ac\MR2\src\Castle.MonoRail。Framework\Attributes\JSONBinderAttribute.cs:C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\ 中 Castle.MonoRail.Framework.SmartDispatcherController.BuildMethodArguments(ParameterInfo[] 参数,IRequest 请求,IDictionary`2 actionArgs) 的第 135 行castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\SmartDispatcherController.cs:line 288
我很确定我的映射和视图模型类是正确的。JQuery 确实发送了预期的 json 对象,但 Monorail 似乎在转换它时遇到了问题。这可能是一个错误吗?
编辑:嗯,Monorail 在这里并没有错……我在帖子中发布的东西毕竟是 JSON。使用此库后:https ://code.google.com/p/jquery-json/ ,我设法将对象转换为 JSON。我认为通过选择 dataType:"json" 就足够了,但显然不是。