在使用 JQGrid 的 MVC 应用程序中 - 如何在控制器操作中设置用户数据

发布于 2024-08-16 10:16:30 字数 762 浏览 3 评论 0原文

如何在控制器操作中设置用户数据。我这样做的方式正在打破我的网格。我正在尝试一个简单的测试,但没有运气。这是我的代码,它不起作用。谢谢。

       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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-08-23 10:16:30

我认为您不必将其转换为数组。我使用过 jqGrid,我只是让 Json 函数序列化对象。我不确定这会导致问题,但至少没有必要。

此外,您的用户数据将计算为字符串(因为您将其作为字符串发送)。尝试将其作为匿名对象发送。即:

userdata = new { test1 = "thefield" },

您需要一个总计值,并在该值和页面之间使用逗号。 (我猜这是一个错字。我认为这不会按原样编译。)

编辑:
另外,我建议将选项“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:

userdata = new { test1 = "thefield" },

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文