为什么即使返回的结果为真且具有值,数据也不会加载到表中?

时间:2018-08-12 11:56:29

标签: jquery asp.net json ajax asp.net-mvc

当我调用存储过程以使其填满表但不起作用时,即在调试时我发现数据已返回并且状态为200 OK。但是,它不会显示在表格中。我何时检查了渲染的数据,然后就在那里了,但样式为display:none;

响应:

{"ReturnStatusJSON":true,"lstDetailedServicesReturned":[{"FromDate":"\/Date(-62135596800000)\/","ToDate":"\/Date(-62135596800000)\/","CustomerName":null,"InvoiceNo":null,"EntryDateTime":"\/Date(-62135596800000)\/","ServiceName":null,"VehicleRegNo":null,"ServicePrice":0,"TotalCost":0,"TotalCommission":1410.0000,"EarnedCommission":0,"Commission":0,"PartyName":"A","PartyServicesTotal":46100.0000,"PartyCommissionTotal":1410.0000,"TotalServices":46100.0000}]}

为什么?

代码:

查看:

@model ZahidCarWash.ViewModels.DetailedServicesReportViewModel

@{
    ViewBag.Title = "PartySalesReport";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


<div id="appointments" class="appointment-main-block appointment-two-main-block pad-container ">

    <div class="container">
        <div class="row" style="margin-top:5% !important">
            <div class="section text-center">
                <h3 class="section-heading text-center">Party Wise Sales Report</h3>

            </div>
            <div class="col-md-8 col-sm-12 GeneralWidth">
                <div class="appointment-block">
                    <form id="appointment-form" class="appointment-form" method="post" action="#">
                        <h5 class="form-heading-title"><span class="form-heading-no">2.</span>Search</h5>
                        <div class="row">

                            @using (Html.BeginForm())
                            {
                                <div class="col-sm-5">
                                    @Html.EditorFor(Model => Model.FromDate, new { htmlAttributes = new { @class = "form-control searchbox-border", placeholder = "From Date" } })
                                    @Html.ValidationMessageFor(Model => Model.FromDate, "", new { @class = "ErrorMessages" })
                                </div>

                                <div class="col-sm-5">
                                    @Html.EditorFor(Model => Model.ToDate, new { htmlAttributes = new { @class = "form-control searchbox-border", placeholder = "To Date" } })
                                    @Html.ValidationMessageFor(Model => Model.ToDate, "", new { @class = "ErrorMessages" })
                                </div>

                                <div class="col-sm-2">
                                    <button type="submit" class="btn btn-default">Search</button>
                                </div>
                            }

                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!--Table-->

    <div class="container">
        <h5 class="form-heading-title"><span class="form-heading-no">2.</span>Party Sales Report</h5>
        <div class="table-responsive">

            <table id="tblPartySalesReport" class="table table-responsive">
                <thead>
                    <tr class="table-header">
                        <th>S.No</th>
                        <th>Party</th>
                        <th>Party Services Total</th>
                        <th>Party Commission Total</th>
                    </tr>
                </thead>
                <tfoot>
                    <tr style="background-color:#e8e8ef">
                        <td></td>
                        <td><b>Total</b></td>
                        <td id="tdTotal" style="font-weight: bold"></td>
                        <td id="tdTotalCommission" style="font-weight: bold"></td>                       
                    </tr>
                </tfoot>
                <tbody id="tbodytblPartySalesReport" class="tableBody"></tbody>

            </table>
        </div>
    </div>
    <!--End data table-->
</div>

@section CustomScripts
{


    <script type="text/javascript">

        var names = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

        function formatJSONDate(date) {
            var MyDate_String_Value = date
            var value = new Date
                        (
                             parseInt(MyDate_String_Value.replace(/(^.*\()|([+-].*$)/g, ''))
                        );

            var month = names[value.getMonth()]; //since months returned from new date start from 0. So, adding 1.

            var dat = value.getDate() +
                                   "/" +
                month +

                                   "/" +
                   value.getFullYear();

            return dat;
        }

        $(document).ready(function () {

            $('#FromDate').datepicker({ format: 'dd/M/yyyy' });
            $('#ToDate').datepicker({ format: 'dd/M/yyyy' });

            $('#FromDate').addClass('searchbox-border');
            $('#ToDate').addClass('searchbox-border');

            $('#tblPartySalesReport').hide();

        });

        $('form').submit(function (e) {
            e.preventDefault();
            if (!$(this).valid()) {
                $("#tbodytblPartySalesReport").html("");
                return;
            }
            else {
                filltblPartySalesReport();
            }
        });

        function filltblPartySalesReport() {

            $('tfoot td#tdTotal').text("");

            var url = '@Url.Action("PartySalesReport")';
            var data = { FromDate: $("#FromDate").val(), ToDate: $("#ToDate").val() }

            $.post(url, data, function (response) {

                if (response.ReturnStatusJSON == true) {

                    $("#tbodytblPartySalesReport").html("");

                    var counter = 1;

                    $.each(response.lstDetailedServicesReturned, function (i, val) {

                        $("#tblPartySalesReport").append($('<tr>').append($('<td>').html(i))
                         .append($('<td>').html(val.PartyName))
                         .append($('<td>').html(val.PartyServicesTotal))
                         .append($('<td>').html(val.PartyCommissionTotal))
                        )

                        i++;

                        $('tfoot td#tdTotal').text(val.TotalServices);
                        $('tfoot td#tdTotalCommission').text(val.TotalCommission);

                        counter = i;
                    })

                    if (counter <= 1)
                    {
                        $('tfoot td#tdTotal').text("");
                        $('tfoot td#tdTotalCommission').text("");
                        return;
                    }

                    $('#tblPartySalesReport').show();

                   // $('#tblPartySalesReport').DataTable().fnDestroy();

                    $('#tblPartySalesReport').DataTable();
                }
                else {

                    swal("Sorry !", "No Record Found", "error");
                    $("#tbodytblPartySalesReport").html("");

                }
            });


        }
    </script>
}

控制器:

 public ActionResult PartySalesReport()
        {
            return View();
        }

        [HttpPost]
        public JsonResult PartySalesReport(DetailedServicesReportViewModel DetailedServicesReportVM)
        {

            List<DetailedServicesReportViewModel> lstPartySales = InvoicesRepository.getPartyWiseSales(DetailedServicesReportVM);

            return Json(new { ReturnStatusJSON = true, lstDetailedServicesReturned = lstPartySales });
        }

DAL:

public static List<DetailedServicesReportViewModel> getPartyWiseSales(DetailedServicesReportViewModel ParamPartyWiseSales)
{
    DataTable dt;
    DetailedServicesReportViewModel PartyWiseSalesReportVM;
    SqlConnection conn = null;
    SqlCommand cmd = null;
    List<DetailedServicesReportViewModel> lstShowPartyWiseSales = new List<DetailedServicesReportViewModel>();

    try
    {
        string ConnectionString = Utility.getConnectionString();

        conn = new SqlConnection(ConnectionString);
        cmd = new SqlCommand("getPartyWiseSales", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@FromDate", ParamPartyWiseSales.FromDate);
        cmd.Parameters.AddWithValue("@ToDate", ParamPartyWiseSales.ToDate);
        SqlDataAdapter Adapter = new SqlDataAdapter(cmd);

        conn.Open();
        dt = new DataTable();
        Adapter.Fill(dt);
        conn.Close();

        foreach (DataRow dr in dt.Rows)
        {
            PartyWiseSalesReportVM = new DetailedServicesReportViewModel();

            PartyWiseSalesReportVM.PartyName = Convert.ToString(dr["PartyName"]);
            PartyWiseSalesReportVM.PartyServicesTotal = Convert.ToDecimal(dr["PartyServicesTotal"]);
            PartyWiseSalesReportVM.PartyCommissionTotal = Convert.ToDecimal(dr["PartyCommissionTotal"]);
            PartyWiseSalesReportVM.TotalServices = Convert.ToDecimal(dr["TotalServices"]);
            PartyWiseSalesReportVM.TotalCommission = Convert.ToDecimal(dr["TotalCommission"]);

            lstShowPartyWiseSales.Add(PartyWiseSalesReportVM);
        }
    }

    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Dispose();
        cmd.Dispose();
    }
    return lstShowPartyWiseSales;
}

存储过程:

ALTER Procedure [dbo].[getPartyWiseSales] 

@FromDate datetime= null,
@ToDate datetime= null

AS
Begin

Select  *  from
    (
        Select ISNULL(a.Party,'') PartyName, ISNULL(a.PartyServicesTotal,0) as PartyServicesTotal, ISNULL(b.PartyCommissionTotal,0) as PartyCommissionTotal, 
        ISNULL(a.TotalServices,0) as TotalServices, ISNULL(b.TotalCommission,0) as TotalCommission
        from 
            (
                Select i.Party , SUM(i.serviceprice) PartyServicesTotal, SUM(SUM(i.serviceprice)) over() as TotalServices from Invoices i
                --where EntryDateTime between Format(@FromDate,'dd/MMM/yyyy') AND Format(@ToDate,'dd/MMM/yyyy') AND (i.Party IS NOT NULL AND i.Party <> '')
                group by i.Party 
            ) a
            Inner Join
            (
                Select i.Party, SUM(i.OwnerCommission) PartyCommissionTotal, SUM(SUM(i.OwnerCommission)) over() as TotalCommission  from Invoices i
                --where EntryDateTime between Format(@FromDate,'dd/MMM/yyyy') AND Format(@ToDate,'dd/MMM/yyyy') AND (i.Party IS NOT NULL AND i.Party <> '')
                group by i.Party
            ) b
            ON a.Party= b.Party
    ) partywisesales

End

0 个答案:

没有答案