jqGrid ASP.NET MVC2 DataBind NullReferenceException
我正在尝试 JQGrid ASP.NET MVC2 版本。
创建了一个模型和控制器:
var itemsGridModel = new OrderItemsjqGridModel();
itemsGridModel.OrderItemsGrid.DataUrl = Url.Action("ItemsDataRequested", new {OrderID = OrderID, CustomerID = CustomerID });
orderVM.orderItemsGrid = itemsGridModel;
return View(orderVM);
ItemsDataRequested:
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;
}
}
I'm trying JQGrid ASP.NET MVC2 version.
Created a model and the controller:
var itemsGridModel = new OrderItemsjqGridModel();
itemsGridModel.OrderItemsGrid.DataUrl = Url.Action("ItemsDataRequested", new {OrderID = OrderID, CustomerID = CustomerID });
orderVM.orderItemsGrid = itemsGridModel;
return View(orderVM);
The ItemsDataRequested:
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);
}
I get a NullReferenceException in the last line.
The myItems has 1 Item with all values filled.
Any Idea what am I doing wrong ?
Thanks.
StackTrace:
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)
Model:
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这是一个老问题,但我一直在寻找同一问题的解决方案,但没有运气,我返回到我的代码进行更多异常分析,我找到了解决方案,我更喜欢将其记录在这里,以便任何人都可以同样的问题就可以得到满足。
我发现了原因,模型 JGridColumn 名称与返回的 SQL 查询字段不同,模型列之一存在拼写错误。
确保所有 JGrid 列的名称与 SQL 查询列的名称相同。
I know this is an old question, but I was looking for a solution for the same problem with no luck, I have returned back to my code for more exception analysis, and I found the solution, I prefered to log it here so anybody has the same problem would be satisfied.
I have discovered the reason, the Model JGridColumn names are different than the returned SQL query fields, there was a spelling mistake in one of the the Model columns.
Make sure all JGrid columns' names are identical with the SQL query columns' names.