繁体   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?

我想使用Ajax请求显示表数据。

这是我的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;
                            }

                        }
                    ]
                });

这是我的操作方法:

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

现在,我在哪里错了?

请插入以下示例。 如果您不介意,请按照我的示例进行操作,然后就可以解决您的问题。

控制器/型号:

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;
    }

视图:

<!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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM