简体   繁体   English

在ASP.NET MVC 5中使用jQuery AJAX在HTML表中不填充Json数据

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

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 我正在创建一个ee门户,用户登录后即可登录。然后,他将进入报告页面。在报告页面中,他可以选择日期范围以获取详细信息,我想显示日期范围搜索数据使用jQuery和Ajax的html表,当我尝试不填充时,我尝试了每种方法,但结果未到

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 我的cshtml视图

<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 () {

        }

    });
});

From what i understand you are sending the start and end date in form of json object. 据我了解,您正在以json对象的形式发送开始和结束日期。 json stores dates in a very different form and its already a string so converting to string wont change the format. json以非常不同的形式存储日期,并且它已经是字符串,因此转换为字符串不会更改格式。

Manually change the date format to dd-mm-yyyy. 手动将日期格式更改为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. 如果您的日期格式不正确,则由于您使用了try catch,因此代码将中断,并且将返回异常消息,而不是您要查找的实际数据,但您并未在视图中显示msg。 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. 并检查它是否继续捕获或中断,在这种情况下,变量ex会保留错误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. 我也建议将ActionResult更改为JsonResult。 Since you are returning Json data. 由于您正在返回Json数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ajax,Jquery,Asp.Net MVC从数据库中获取数据并以表格式显示数据 - Fetching the Data from db and displaying it in table format using Ajax, Jquery, Asp.Net MVC 在ASP.NET MVC中使用Ajax jQuery图像未插入数据 - Image is not inserted with data using ajax jquery in asp.net MVC ASP.NET MVC / Jquery Unobtrusive Ajax,在返回html数据后尝试检查元素是否加载了Jquery - ASP.NET MVC / Jquery Unobtrusive Ajax, trying to check if element is loaded with Jquery after the html data is returned ASP.NET MVC CORE fails model binding TypeScript Json data through Jquery.ajax() - ASP.NET MVC CORE fails model binding TypeScript Json data through Jquery .ajax() ASP.Net MVC和JQuery Ajax-页面被json结果替换 - ASP.Net MVC & JQuery Ajax - page is replaced with json result 我想在Asp.Net MVC中使用Ajax显示表数据。 这种Ajax语法有什么问题? - I want to show table data using Ajax in Asp.Net MVC. What is wrong with this Ajax syntax? 使用Jquery / Ajax在表内创建if语句的语法-ASP.NET MVC - Syntax for an if statement inside an Table Using Jquery/Ajax - ASP.NET MVC 使用jQuery Ajax的ASP.Net MVC 5无法发送__RequestVerificationToken - ASP.Net MVC 5 using jQuery Ajax unable to send __RequestVerificationToken Jquery $ .ajax没有在ASP.NET MVC中发布一串数据 - Jquery $.ajax not posting a string of data in ASP.NET MVC 在 ASP NET MVC 和 JQUERY/AJAX 中将 html 表的值保存到数据库 - Saving values of html table to database in ASP NET MVC and JQUERY/AJAX
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM