在使用 JQGrid 的 MVC 应用程序中 - 如何在控制器操作中设置用户数据
如何在控制器操作中设置用户数据。我这样做的方式正在打破我的网格。我正在尝试一个简单的测试,但没有运气。这是我的代码,它不起作用。谢谢。
var dataJson = new
{
total =
page = 1,
records = 10000,
userdata = "{test1:thefield}",
rows = (from e in equipment
select new
{
id = e.equip_id,
cell = new string[] {
e.type_desc,
e.make_descr,
e.model_descr,
e.equip_year,
e.work_loc,
e.insp_due_dt,
e.registered_by,
e.managed_by
}
}).ToArray()
};
return Json(dataJson);
How do you set the userdata in the controller action. The way I'm doing it is breaking my grid. I'm trying a simple test with no luck. Here's my code which does not work. Thanks.
var dataJson = new
{
total =
page = 1,
records = 10000,
userdata = "{test1:thefield}",
rows = (from e in equipment
select new
{
id = e.equip_id,
cell = new string[] {
e.type_desc,
e.make_descr,
e.model_descr,
e.equip_year,
e.work_loc,
e.insp_due_dt,
e.registered_by,
e.managed_by
}
}).ToArray()
};
return Json(dataJson);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不必将其转换为数组。我使用过 jqGrid,我只是让 Json 函数序列化对象。我不确定这会导致问题,但至少没有必要。
此外,您的用户数据将计算为字符串(因为您将其作为字符串发送)。尝试将其作为匿名对象发送。即:
您需要一个总计值,并在该值和页面之间使用逗号。 (我猜这是一个错字。我认为这不会按原样编译。)
编辑:
另外,我建议将选项“jsonReader: { Repeatitems: false }”添加到您的 JavaScript 中。这将允许您在“行”字段中发送集合,而无需将其转换为“{id: ID, cell: [ data_row_as_array ] }”语法。您可以在 colModel 中设置属性“key = true”来指示哪个字段是 ID。它使得将数据传递到网格变得更加简单。
I don't think you have to convert it to an Array. I've used jqGrid and i just let the Json function serialize the object. I'm not certain that would cause a problem, but it's unnecessary at the very least.
Also, your user data would evaluate to a string (because you are sending it as a string). Try sending it as an anonymous object. ie:
You need a value for total and a comma between that and page. (I'm guessing that's a typo. I don't think that would compile as is.)
EDIT:
Also, i would recommend adding the option "jsonReader: { repeatitems: false }" to your javascript. This will allow you to send your collection in the "rows" field without converting it to the "{id: ID, cell: [ data_row_as_array ] }" syntax. You can set the property "key = true" in your colModel to indicate which field is the ID. It makes it a lot simpler to pass data to the grid.