使用subGridrowExpanded的MVC3 jqGrid子网格

时间:2012-10-05 16:14:16

标签: asp.net-mvc-3 razor jqgrid parameter-passing

我正在开发一个MVC3 / razor应用程序,我正在尝试使用subGridRowExpanded将jqGrid嵌套在另一个内部(所以我可以使用格式化程序),如何传递父网格的id值到儿童网格?

以下代码运行主网格,并使用“搜索/客户”URL中的数据填充它,但我不知道如何在“搜索/银行链接”URL控制器中选择我选择的记录

谁能告诉我怎么做?

提前致谢 安德鲁。

我的Index.cshtml

@{
    ViewBag.Title = "Index";
}

<h2>@ViewBag.Message</h2>
<div id="content">
    <div id="content-left">
        @Html.Partial("SearchPanel")
    </div>
    <div id="content-main">
        <table id="jpgCustomers" cellpadding="0" cellspacing="0"></table>
        <div id="jpgpCustomers" style="text-align:center;"></div>
    </div>
</div>
@section JavaScript
{
<script type="text/javascript">
    $(document).ready(function ()
    {
        $('#jpgCustomers').jqGrid({
            //url from wich data should be requested
            url: '@Url.Action("Customers")',
            datatype: 'json',
            mtype: 'POST',
            colNames: ['Name', 'FullName', 'SFTP Enabled', 'IsTranbase'],
            colModel: [
                { name: 'LogonName', index: 'LogonName', align: 'left' },
                { name: 'FullName', index: 'FullName', align: 'left' },
                { name: 'Enabled', index: 'Enabled', align: 'left' },
                { name: 'IsTran', index: 'IsTranbase', align: 'left' }
            ],
            pager: $('#jpgpCustomers'),
            rowNum: 10,
            sortname: 'FullName',
            sortorder: 'asc',
            viewrecords: true,
            height: '100%',
            subGrid: true,
            subGridRowExpanded: function(subgrid_id, row_id) {
                var subgrid_table_id, pager_id;
                subgrid_table_id = subgrid_id+"_t";
                pager_id = "p_"+subgrid_table_id;
                $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"'class='scroll'></table><div id='"+pager_id+"'class='scroll'></div>");
                $("#"+subgrid_table_id).jqGrid({
                    url: '@Url.Action("BankLinks")',
                    datatype: 'json',
                    mtype: 'POST',
                    colNames: ['Bank', 'Folder', 'Enabled'],
                    colModel:[
                        {name:"Bank",index:"Bank",width:80,key:true},
                        {name:"Folder",index:"Folder",width:130},
                        {name:"Enabled",index:"Enabled",width:70,align:"left"}
                    ],
                    rowNum:20,
                    pager: pager_id,
                    sortname: 'Bank',
                    sortorder: "asc",
                    viewrecords: true,
                    height: '100%'
                });
                $("#"+subgrid_table_id).jqGrid('navGrid',"#"+pager_id,{edit:false,add:false,del:false})
            },
        });
    });
</script>
}

我的控制器操作:

客户

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Customers(JqGridRequest request)
{
    ISession session = NHibernateHelper.GetCurrentSession();
    IEnumerable<Customer> customers = session.QueryOver<Customer>().List().Skip<Customer>(0).Take<Customer>(request.RecordsCount);

    int totalRecords = customers.Count();

    //Prepare JqGridData instance
    JqGridResponse response = new JqGridResponse()
    {
        //Total pages count
        TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
        //Page number
        PageIndex = request.PageIndex,
        //Total records count
        TotalRecordsCount = totalRecords
    };
    //Table with rows data
    foreach (Customer customer in customers)
    {
        response.Records.Add(new JqGridRecord(Convert.ToString(customer.Id), new List<object>()
        {
            customer.FtpDetails.LogonName,
            customer.FtpDetails.FullName,
            customer.FtpDetails.Enabled,
            customer.IsTran
        }));
    }

    //Return data as json
    return new JqGridJsonResult() { Data = response };
}

BankLinks

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult BankLinks(JqGridRequest request)
{
    ISession session = NHibernateHelper.GetCurrentSession();

    //IN THIS LINE I HAVE HARDCODED value 14 - I need this value to be passed from the 
    //Parent Grid!
    Customer customer = session.Get<Customer>(14);

    int totalRecords = customer.Banks.Count();

    //Prepare JqGridData instance
    JqGridResponse response = new JqGridResponse()
    {
        //Total pages count
        TotalPagesCount = (int)Math.Ceiling((float)totalRecords / (float)request.RecordsCount),
        //Page number
        PageIndex = request.PageIndex,
        //Total records count
        TotalRecordsCount = totalRecords
    };

    foreach (Bank bank in customer.Banks.ToList<Bank>())
    {
        CustomerBank bankLink = session.QueryOver<CustomerBank>().Where(x => x.BankId == bank.Id).Where(y => y.CustomerId == customer.Id).List<CustomerBank>().FirstOrDefault();

        response.Records.Add(new JqGridRecord(null, new List<object>()
        {
            bank.BankCode,
            bank.Folder,
            bankLink.Enabled 
        }));
    }
     return new JqGridJsonResult() { Data = response };
 }

1 个答案:

答案 0 :(得分:1)

首先,您应该像这样修改subGridRowExpanded回调:

subGridRowExpanded: function(subgrid_id, row_id) {
    var subgrid_table_id, pager_id;
    subgrid_table_id = subgrid_id + '_t';
    pager_id = "p_"+subgrid_table_id;
    $('#' + subgrid_id).html('<table id="' + subgrid_table_id + '" class="scroll"></table><div id="' + pager_id + '" class="scroll"></div>');
    $('#' + subgrid_table_id).jqGrid({
        url: encodeURI('@Url.Action("BankLinks")' + '?id=' + row_id),
        datatype: 'json',
        mtype: 'POST',
        colNames: ['Bank', 'Folder', 'Enabled'],
        colModel: [
            {name: 'Bank', index: 'Bank', width:80, key:true },
            {name: 'Folder', index:'Folder', width:130 },
            {name: 'Enabled', index: 'Enabled', width:70, align: 'left' }
        ],
        rowNum: 20,
        pager: pager_id,
        sortname: 'Bank',
        sortorder: 'asc',
        viewrecords: true,
        height: '100%'
    });
    $('#' + subgrid_table_id).jqGrid('navGrid', '#' + pager_id, {edit: false, add: false, del: false })
}

请注意encodeURI的使用,以确保生成的网址有效。

现在您可以像这样修改您的操作方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult BankLinks(int id, JqGridRequest request)
{
    //You can sue id parameter to get the data for selected client.
    ...
}

这应该可以解决问题。