ASP.NET MVC + jqGrid的无AJAX [英] ASP.NET MVC + jqGrid without AJAX

查看:347
本文介绍了ASP.NET MVC + jqGrid的无AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在执行针对产品数据库检索ASP.NET MVC应用程序。我想显示在使用的TreeGrid模块的jqGrid结果。我并不真的需要网格可AJAX-Y,因为数据是静态的,它足够小,它可以一次全部被发送到客户端。

I have an ASP.NET MVC application which is executing a search against a products database. I want to display the results in a jqGrid using the TreeGrid module. I don't really need the grid to be AJAX-y because the data is static and it is small enough that it can all be sent to the client at once.

第一个问题:我该如何设置jqGrid的,这样,而不是从它只是看起来在一个JS变量或东西URL拉动JSON数据

First question: how do I set up jqGrid so that instead of pulling the JSON data from a URL it just looks in a JS variable or something?

其次,什么是最合适的方式来获得ASP.NET MVC把JSON数据转换为JavaScript变量?我已经在我的控制器名单,只是想以某种方式把它弄出来成JSON-ING后一个JS变量。

Secondly, what is the most appropriate way to get ASP.NET MVC to put JSON data into a JavaScript variable? I already have the List in my controller and just want to somehow get it out into a JS variable after JSON-ing it.

还是我对抗当前的太多,只是接受的jqGrid似乎想工作?在AJAX-Y方式

Or am I fighting against the current too much and just accept the AJAX-y way that jqGrid seems to want to work?

谢谢,

〜贾斯汀

推荐答案

下面是如何使用JavaScript函数来显示jqGrid的树。

Here is how to display a jqGrid tree using a JavaScript function.

$(document).ready(function() {
    TreeDemo.setupGrid($("#tree"));
});

TreeDemo = {
    data: { A: ["A1", "A2"], B: ["B1", "B2"] },
    setupGrid: function(grid) {
        grid.jqGrid({
            colNames: ['Name'],
            colModel: [
                  { name: 'Name', index: 'Name', width: "250em" }
                ],
            datatype: TreeDemo.treeData,
            loadui: "none",
            sortname: 'Number',
            treeGrid: true,
            treeGridModel: "adjacency",
            sortorder: "asc"
        })
    },
    treeData: function(postdata) {
        var items = postdata.nodeid ? TreeDemo.data[postdata.nodeid] : TreeDemo.data;
        var i = 0;
        var rows = new Array();
        for (val in items) {
            var isLeaf = postdata.nodeid != undefined;
            rows[i] = {
                Name: val,
                Id: val,
                level: postdata.nodeid ? 1 : 0,
                parent: postdata.nodeid || null,
                isLeaf: isLeaf ? "true" : "false",
                expanded: "false"
            }
            i++;
        }
        $("#tree")[0].addJSONData({
            Total: 1,
            Page: 1,
            Records: 2,
            Rows: rows
        });
    }
};

请注意,有很多的选择你如何做到这一点,我的例子只有一个。

Note that there are lots of options for how you do this and my example is only one.

我会得到JSON成一个JS变种的方法是两种:

The way I would get the JSON into a JS var is to either:


  1. 写HTML辅助发射一个短脚本的页面。

  2. 写它返回一个动作 JavaScriptResult 来得到一个文件中的数据,如果由于某种原因,你不能有数据内嵌。

  1. Write a HTML Helper which emits a short script to the page.
  2. Write an action which returns a JavaScriptResult to get the data in a file, if, for some reason, you can't have the data inline.

您创建一个使用.NET的JavaScript序列化的JSON。在MVC源$ C ​​$ C为例看 JsonResult.ExecuteResult

You create the JSON using the .NET JavaScript serializer. Look at the JsonResult.ExecuteResult in the MVC source code for an example.

这篇关于ASP.NET MVC + jqGrid的无AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆