简体   繁体   English

我想在Asp.Net MVC中使用Ajax显示表数据。 这种Ajax语法有什么问题?

[英]I want to show table data using Ajax in Asp.Net MVC. What is wrong with this Ajax syntax?

I want to show table data using Ajax request. 我想使用Ajax请求显示表数据。

Here is my Ajax Syntax: 这是我的Ajax语法:

  $(document).ready(function() {
                var table = $("#attendance").DataTable({
                    ajax: {
                        url: '@Url.Action("GetAttendance", new {id = Model.Student.Id})',
                        dataSrc: ""
                    },
                    columns: [
                        {
                            data: "Date",
                            render: function(data) {
                                return data;
                            }
                        },
                        {
                            data: "Status.StudentStatus",
                            render: function(data) {
                                return data;
                            }

                        }
                    ]
                });

Here is my action method: 这是我的操作方法:

  public JsonResult GetAttendance(int id)
    {
        var studentAttendance = _context.Attendances.ToList().Where(m => m.StudentId == id);
        return Json(studentAttendance, JsonRequestBehavior.AllowGet);
    }

Now, where am I wrong? 现在,我在哪里错了?

Please plug in the following example. 请插入以下示例。 If you don't mind, please just follow my example, and then you will be able to fix your issue. 如果您不介意,请按照我的示例进行操作,然后就可以解决您的问题。

Controller/Model: 控制器/型号:

public class AjaxViewModel
{
    public string theDate { get; set; }
    public string StudentStatue { get; set; }
}

public class HomeController : Controller
{
    public string GetAttendance()
    {
        AjaxViewModel aViewModel = new AjaxViewModel { StudentStatue = "stat4", theDate = "12/24/2005" };
        AjaxViewModel aViewModel2 = new AjaxViewModel { StudentStatue = "stat5", theDate = "12/24/2005" };
        AjaxViewModel aViewModel3 = new AjaxViewModel { StudentStatue = "stat6", theDate = "12/24/2005" };

        IList<AjaxViewModel> data = new List<AjaxViewModel>();
        data.Add(aViewModel);
        data.Add(aViewModel2);
        data.Add(aViewModel3);

        JavaScriptSerializer js = new JavaScriptSerializer();
        string json = js.Serialize(data);
        json = "{ \"data\": " + json;
        json = json + " }";
        return json;
    }

View: 视图:

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index2020</title>
    <script src="~/Scripts/jquery-1.12.4.min.js"></script>
    <link href="~/Content/DataTables/css/jquery.dataTables.min.css" rel="stylesheet" />
    <script src="~/Scripts/DataTables/jquery.dataTables.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#uiDataTable").DataTable({
                "ajax": '@Url.Action("GetAttendance")',
                columns: [
                    {
                        "data": "theDate"
                    },
                    {
                        "data": "StudentStatue"
                    }
                ]
            });
        })
    </script>
</head>
<body>
    <table id="uiDataTable" class="display table table-striped table-bordered">
        <thead>
            <tr>
                <th>
                    Date
                </th>
                <th>
                    Status
                </th>

            </tr>
        </thead>
    </table>
</body>
</html>

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

相关问题 使用ajax post将数据从JS传输到asp.net mvc中Controller中的函数。 但观点没有改变 - Using ajax post to transfer data from JS to a function in Controller in asp.net mvc. But the view is not changed 在jQuery Ajax调用之后,应用程序无法导航到我要显示的ASP.NET MVC视图。 - After jQuery Ajax invocation call , the application Fails to navigate to the ASP.NET MVC view that I want to show 使用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 的正确模式是什么? - What is the right pattern for using JQuery Ajax and ASP.Net Mvc? 在ASP.NET MVC中使用Ajax jQuery图像未插入数据 - Image is not inserted with data using ajax jquery in asp.net MVC 在Modal ASP.NET MVC中使用Ajax添加数据 - Add data using ajax in Modal asp.net mvc 在ASP.NET MVC 5中使用jQuery AJAX在HTML表中不填充Json数据 - Json data is not populat in html table using jQuery ajax in asp.net mvc 5 使用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无法加载数据 - asp.net mvc Ajax not loading data 调用AJAX(ASP.NET MVC)后无法显示值 - Cannot show value after AJAX call (ASP.NET MVC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM