gpt4 book ai didi

c# - Monorail 无法读取从 jQuery 接收到的 JSON(对象列表)数据

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:00 27 4
gpt4 key购买 nike

我有以下 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 Controller 。

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
}

它给我的响应是这样的:

Message: Error building method arguments. Last param analyzed was neworder with value ''

StackTrace: at Castle.MonoRail.Framework.SmartDispatcherController.BuildMethodArguments(ParameterInfo[] parameters, IRequest request, IDictionary2 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 1639

Inner 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

我很确定我的映射和 View 模型类是正确的。 JQuery 确实发送了预期的 json 对象,但 Monorail 似乎在转换它时遇到问题。这可能是一个错误吗?

编辑:好吧,单轨铁路并没有错...毕竟我在帖子中放的东西是 WERENT A JSON。使用此库后:https://code.google.com/p/jquery-json/ ,我设法将对象转换为 JSON。我认为通过选择 dataType:"json"就足够了,但显然不是。

最佳答案

$.ajax 调用中的数据类型选项指的是预期的数据。你需要做的是:

$.ajax({
url: "/Photos/SetOrder",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({neworder:orderState}),
type: 'POST'
});

关于c# - Monorail 无法读取从 jQuery 接收到的 JSON(对象列表)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14960512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com