Json data is not populat in html table using jQuery ajax in asp.net mvc 5

asp.net-ajax asp.net-mvc entity-framework-6 javascript jquery

Question

I am creating a ee portal, where user is login, once he log in ..then he will come to report page.. In report page he have option to select date range to fetch details, I want to show date range search data in html table using jQuery and ajax, When i am trying it is not populate , I was try each one method but result is not coming

My controller

[HttpGet]
    public ActionResult HwportalGetData(DateTime startDate, DateTime endDate)
    {
        SCAN_HW_PortalEntities db = new SCAN_HW_PortalEntities();



        try
        {

            string _startDate = startDate.ToString("dd-MMM-yyyy");
            string _endDate = endDate.AddDays(1).ToString("dd-MMM-yyyy");




            var result = db.Scan_HW_Terminal.Where(x => x.CreatDate >= startDate && x.CreatDate <= endDate).OrderBy(x => x.EmpID).ToList();

            if (result.ToList().Count > 0)
            {
                TempData["total"] = result.Count.ToString();
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
                return Json("Invalid");
        }
        catch (Exception ex)
        {
            return Json(ex);
        }

        // return Json(db.Scan_HW_Terminal.ToList());
    }

}

}

My cshtml view

<div class="grid-container" id="DataShowGride">
                    <Table Class="table table-bordered" id="myTable">
                        <thead>
                            <tr>
                                <th>Employee ID</th>
                                <th>Create Date</th>
                                <th>City Name</th>
                                <th>Device Type</th>
                                <th>Status</th>
                                <th>Team Leader Name</th>
                                <th>Serial Number</th>
                            </tr>
                        </thead>

                    </Table>
                </div>

<script type="text/javascript">

$('#btnShow').click(function (e) {
    debugger;
    var mFromDate = $("#FromDate").val();
    var mToDate1 = $("#ToDate1").val();
    //$('#loader').show();
    $.ajax({
        url: '@Url.Action("HwportalGetData", "Home")',
        type: "GET",
        data: {startDate: mFromDate, endDate: mToDate1 },
        //startDate: mFromDate, endDate: mToDate1
        dataType: 'json',
        success: function (data) {
            var tr;
            //Append each row to html table  
            for (var i = 0; i < json.length; i++) {
                tr = $('<tr/>');
                tr.append("<td>" + json[i].EmpID + "</td>");
                tr.append("<td>" + json[i].CreatDate + "</td>");
                tr.append("<td>" + json[i].City + "</td>");
                tr.append("<td>" + json[i].DeviceType + "</td>");
                tr.append("<td>" + json[i].DeviceStatus + "</td>");
                tr.append("<td>" + json[i].TeamLeader + "</td>");
                tr.append("<td>" + json[i].TerminalSlno + "</td>");
                $('#myTable').append(tr);

            }
        },
        error: function () {

        }

    });
});
1
0
9/27/2018 3:07:48 PM

Accepted Answer

From what i understand you are sending the start and end date in form of json object. json stores dates in a very different form and its already a string so converting to string wont change the format.

Manually change the date format to dd-mm-yyyy. Also it hard to tell whether this is your actual problem so before that i would recommend debugging your controller code and see in what format is your date being displayed.

In case your date format is wrong your code will break since you have used try catch and will return an exception message instead of the actual data you are looking for but you haven't displayed the msg in your view. place a break point on this line of code:

     var result = db.Scan_HW_Terminal.Where(x => x.CreatDate >= startDate && x.CreatDate <= endDate).OrderBy(x => x.EmpID).ToList();

and check whther it continues or breaks to catch, in which case the variable ex will ahve the error msg. check what the error is. If i am right using the above method should solve your problem.

Also i would recommend changing ActionResult to JsonResult. Since you are returning Json data.

0
9/28/2018 2:00:51 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow