【问题标题】:generate jqgrid using model id / pass id to controller's post method to generate jqgrid使用模型 id 生成 jqgrid / 将 id 传递给控制器​​的 post 方法以生成 jqgrid
【发布时间】:2014-09-13 02:41:22
【问题描述】:

我想知道如何通过将 Id 传递给控制器​​的 post 方法来生成 jqgrid? jqgrid 中是否有任何 postdata 类型的属性可用?请给我详细的示例以根据特定的 id 生成 jqgrid 以及如何在控制器的 post 方法中获取此 ID?

让我再澄清一下……

这里是返回 View 的控制器方法:

public ActionResult Index1(int Id)
        {
            ViewBag.Id = Id;
            return View("Index");
        }

现在这个索引视图在这里:

 $(document).ready(function () {

    $("#AssetTable").jqGrid({
            url: '/Controller/List',
            mtype: 'POST',
            datatype: "json",
            jsonReader: {
                root: "rows", //array containing actual data
                page: "page", //current page
                total: "total", //total pages for the queryd
                records: "records", //total number of records
                repeatitems: false,
                id: "ID" //index of the column with the PK in it
            },
            colNames: ['ID', 'Dedicated', 'Post Of'],
            colModel: [
                { name: 'ID', width: 150, formatter: myLinkFormatter },
                { name: 'Dedicated', width: 300},
                { name: 'PostOf', width: 300 }


            ],
            rowNum: 10,
            rowList: [10, 15, 20],
            pager: '#AssetTablePager',
            sortname: 'Dedicated',
            viewrecords: true,
            sortorder: "asc",
            caption: "Armories",
            height: "auto",
            width: "auto",
            altRows: true,
            altclass: 'OddGridRow'
    });

这是我的控制器的 post 方法,它生成 jqgrid 这里我想要 Id 作为参数,因为我想根据这个 id 生成网格

public override JsonResult List(int? page, int? rows, string sidx, string sord, bool _search, string searchField, string searchOper, string searchString)
    {
        var _db = new AOAEntities();
        // Create the Repository and get the Model
        List<vendor> _data = new VendorRepository().GetList();

        //load the Model in to the ViewModel

        List<VendorVM> _vendors = _data.Select(x => new VendorVM
        {
            ID = x.id_vendor,
            Name = x.nm_vendor.Trim(),
            DefaultObjectCode = x.object_codes == null ? "" : x.object_codes.ds_code.Trim() + "-" + x.object_codes.ds_description.Trim(),
            AddressLine1 = x.ds_address_line1.Trim(),

            City = x.ds_city.Trim(),

            Phone = x.ds_phone.Trim()




        }).ToList();

        int _page = page ?? 1;
        int _rows = rows ?? 1;

        if (_search)
        {
            _vendors = _vendors.AsQueryable().Where(searchField, searchString, searchOper).ToList();
        }

        var sortedAssets = _vendors.AsQueryable().SortBy(string.Format("{0} {1}", sidx, sord));

        sortedAssets = sortedAssets.Skip((_page - 1) * _rows).Take(_rows);
        var pagedAssets = (_vendors.Count / rows) + (_vendors.Count % rows == 0 ? 0 : 1);

        var results = new
        {
            total = pagedAssets, //number of pages
            page = page, //current page
            records = _vendors.Count(), //total items
            rows = sortedAssets
        };

        return Json(results);
    }

我的问题是如何在这个方法中获取id(public override JsonResult List)

【问题讨论】:

  • 请告诉我们你到目前为止做了什么。
  • 绝对不清楚您需要什么。您的意思是哪个 id(某行的 id、网格的 id、主网格的行的 id 以填充第二个网格的内容等)?您在填充网格或编辑网格时遇到问题吗?

标签: jquery asp.net asp.net-mvc asp.net-mvc-4 jqgrid


【解决方案1】:

您可以使用 Url.Action 助手包含 id 参数。观点:

...
url: $("#GetQueueGridAction").data("url"),
...

<div id="GetQueueGridAction" data-url="@Url.Action("GetAction", "Controller", new { id = ViewBag.Id })"></div>

然后在控制器方法签名中:

public JsonResult GetQueue(int id, string sidx, string sord, int page, int rows)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    相关资源
    最近更新 更多