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

标签 javascript jquery ajax asp.net-mvc datatable

我想使用 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);
    }

现在,我哪里错了?

最佳答案

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

Controller /模型:

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>

关于javascript - 我想在 Asp.Net MVC 中使用 Ajax 显示表数据。这个 Ajax 语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638186/

相关文章:

javascript - 在继续 AJAX post 之前检查验证

java - JQuery AJAX 无法获得成功回调函数

javascript - Javascript 中的闭包 : assigning local variable doesn't work

javascript - Promise 运行成功和失败回调

javascript - Bootstrap jalali日期选择器选择年份限制

javascript - 是否可以使用 JavaScript 直接进入打印过程而不加载 html 页面?

javascript - 如何将事件从 Redux 传递给 React?

javascript - 需要指定屏幕 react native tabnavigator

jQuery click() 在 Greasemonkey/Tampermonkey 脚本中不起作用

php - 在 X-Editable 中通过 Ajax 提交数据