gpt4 book ai didi

asp.net - jqGrid ASP.NET MVC2 数据绑定(bind) NullReferenceException

转载 作者:行者123 更新时间:2023-12-05 01:30:45 25 4
gpt4 key购买 nike

我正在尝试 JQGrid ASP.NET MVC2 版本。

创建模型和 Controller :

    var itemsGridModel = new OrderItemsjqGridModel();
itemsGridModel.OrderItemsGrid.DataUrl = Url.Action("ItemsDataRequested", new {OrderID = OrderID, CustomerID = CustomerID });
orderVM.orderItemsGrid = itemsGridModel;


return View(orderVM);

请求的项目数据:

public JsonResult ItemsDataRequested(int OrderID, int CustomerID)
{
// Get both the grid Model and the data Model

var gridModel = new OrderItemsjqGridModel();
var orderItems = ordersRepository.GetOrder(OrderID).ItemsInOrders.ToList();

IQueryable<ItemsInOrder> myItems = orderItems.AsQueryable<ItemsInOrder>();

return gridModel.OrderItemsGrid.DataBind(myItems);
}

我在最后一行得到一个 NullReferenceException。myItems 有 1 个项目,所有值都已填充。

知道我做错了什么吗?

谢谢。

堆栈跟踪:

   at Trirand.Web.Mvc.Util.PrepareJsonResponse(JsonResponse response, JQGrid grid, DataTable dt)
at Trirand.Web.Mvc.Util.ConvertToJson(JsonResponse response, JQGrid grid, DataTable dt)
at Trirand.Web.Mvc.JQGrid.FilterDataSource(Object dataSource, NameValueCollection queryString, IQueryable& iqueryable)
at Trirand.Web.Mvc.JQGrid.GetJsonResponse()
at Trirand.Web.Mvc.JQGrid.DataBind()
at Trirand.Web.Mvc.JQGrid.DataBind(Object dataSource)
at TechRun.UI.Controllers.OrdersController.ItemsDataRequested(Int32 OrderID, Int32 CustomerID) in y:\work\yosi\source\Techrun\TechRun.UI\Controllers\OrdersController.cs:line 118
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassd.<InvokeActionMethodWithFilters>b__a()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)

型号:

public class OrderItemsjqGridModel
{
public JQGrid OrderItemsGrid { get; set; }

public OrderItemsjqGridModel()
{
OrderItemsGrid = new JQGrid
{
Columns = new List<JQGridColumn>()
{
new JQGridColumn { DataField = "OrderID",
// always set PrimaryKey for Add,Edit,Delete operations
// if not set, the first column will be assumed as primary key
PrimaryKey = true,
Editable = false,
Width = 30 },
new JQGridColumn { DataField = "ItemID",
PrimaryKey = true,
Editable = true,
Width = 120},
new JQGridColumn { DataField = "ItemDescription",
Editable = true,
Width = 300},
new JQGridColumn { DataField = "NumOfItems",
Editable = true,
Width = 50
},
new JQGridColumn { DataField = "CostOfItem",
Editable = true,
Width = 75 },
new JQGridColumn { DataField = "TotalCost",
Editable = false
},
new JQGridColumn { DataField = "SuppolyDate",
Editable = true,
Width = 120,
DataFormatString = "{0:d}" },
},
Width = Unit.Pixel(640),
Height = Unit.Percentage(100)
};

OrderItemsGrid.ToolBarSettings.ShowRefreshButton = true;
OrderItemsGrid.ToolBarSettings.ShowAddButton = true;
OrderItemsGrid.ToolBarSettings.ShowDeleteButton = true;
OrderItemsGrid.ToolBarSettings.ShowEditButton = true;
}

}

最佳答案

我知道这是一个老问题,但我一直在寻找相同问题的解决方案,但没有成功,我返回到我的代码进行更多异常分析,我找到了解决方案,我更愿意在这里记录它所以遇到同样问题的任何人都会感到满意。

我发现了原因,模型 JGridColumn 名称与返回的 SQL 查询字段不同,其中一个模型列中存在拼写错误。

确保所有 JGrid 列的名称与 SQL 查询列的名称相同。

关于asp.net - jqGrid ASP.NET MVC2 数据绑定(bind) NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4646453/

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