【问题标题】:passing an object with json data传递带有 json 数据的对象
【发布时间】:2012-09-30 10:13:43
【问题描述】:

我正在使用jsonData 绑定到jqgrid,但我也想在字段中使用model.total amount

那么我怎样才能将它与jsonData 一起返回以在此<td> 中使用它?

查看

<table>
<tr>

<td>@Model.TotalAmount</td>

</tr>
</table>

控制器

 public ActionResult GetDocPendingDetails(string strPA, string strEmpCode, string StrModeOfPayment) // Sends PA to bind the gridview
        {
            string StrEmpcode = ""; // to be changed
            cmn_top objCmn_top = new cmn_top();
            DataTable dtData = new DataTable();
            Items objitem = new Items();
            if (StrModeOfPayment == "All")
            {
                StrModeOfPayment = "";
            }
            dtData = objCmn_top.Reports_GetData__DocPendingPayment(strPA, StrEmpcode, StrModeOfPayment);
            ViewBag.TotalAmount = dtData.AsEnumerable().Sum(x => x.Field<double>("fl_approvedamt")).ToString();           // to be changed later
            int totalRecords = dtData.Rows.Count;
            var jsonData = new
            {
                totalamount = ViewBag.TotalAmount,
                total = totalRecords,
                rows =
                (
                from dtRow in dtData.AsEnumerable()
                select new
                {
                    cell = new string[]
                    {                       
                       dtRow.Field<string>("ch_docno"),dtRow.Field<string>("ch_empcode"),dtRow.Field<string>("ch_sapvoucherno"), dtRow.Field<string>("CH_PSA_CODE"),dtRow.Field<double>("fl_approvedamt").ToString()
                    }
                }
                )
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);

        }

这是我的 jqgrid

    $("#list").GridUnload();
    $("#list").jqGrid({
        url: '/Reports/GetDocPendingDetails',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Document No.', 'Document Owner', 'SAP Document No', 'PSA', 'Approved Amount'],
        colModel: [
              { name: 'ch_docno', index: 'ch_docno', width: 100, align: 'center' },
              { name: 'ch_empcode', index: 'ch_empcode', width: 250, align: 'left' },
              { name: 'ch_sapvoucherno', index: 'ch_sapvoucherno', width: 100, align: 'center' },
              { name: 'CH_PSA_CODE', index: 'CH_PSA_CODE', width: 200, align: 'left' },
                 { name: 'fl_approvedamt', index: 'fl_approvedamt', width: 100, align: 'center' }

            ],
        emptyrecords: "Empty records",
        height: 'auto',
        loadtext: 'Loading.....',
        sortorder: 'asc',
        caption: " Documents Pending For Payment ",
        shrinkToFit: true,
        rownumbers: true,
        rowNum: -1,
        postData: { strPA: selectedPA, StrModeOfPayment: modeOfPayment },
        viewrecords: true,
        gridComplete: function () {
            var recs = parseInt($("#list").getGridParam("records"));
            if (isNaN(recs) || recs == 0) {
                $("#gridWrapper").hide();
                $("#DivEmptyRecords").show();

            }
            else {
                $('#gridWrapper').show();
                $("#DivEmptyRecords").hide();

            }
        }
    });
//document.getElementById("tdTotalAmount").innerHTML = jsonData.TotalAmount;

}

【问题讨论】:

    标签: json asp.net-mvc-3 view controller


    【解决方案1】:

    您要返回 JsonData 对吗?在这种情况下,您不能使用 @Model.TotalAmount

    如果您想使用@Model.TotalAmount,让您的操作返回一个模型,其中包含 TotalAmount 作为属性。

    但由于您需要使用 Jqgrid,我建议您坚持返回 Json 数据。

    我还可以看到您将 totalamount 作为 Json 结果的一部分传递。

    使用 firefox 的 firebug 并调查 json 数据的发布方式和内容,然后将这些值与 jquery 和 jqgrid 一起使用。

    如果你也能发布一些你的 Jqgrid 代码会更好。

    【讨论】:

    • 嗨,我已经发布了 jqgrid 代码,我想在 @model.totalamount 的地方使用 TotalAmount 。我只是很困惑如何在那个地方访问 json 数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 2019-09-27
    • 2020-11-08
    相关资源
    最近更新 更多