简体   繁体   English

为什么dataTables不能与ASP.NET MVC 4 Razor一起使用

[英]Why won't dataTables work with ASP.NET MVC 4 Razor

I'm trying to use dataTables.js with my ASP.NET 4 MVC project. 我正在尝试将dataTables.js与我的ASP.NET 4 MVC项目一起使用。 I have tables that get data from the controller and then generate a table with Razor syntax. 我有表从控制器获取数据,然后生成一个带有Razor语法的表。 Since it's C#, it's server-side, so it should work. 因为它是C#,它是服务器端,所以它应该工作。 Below is my code, can somebody tell me what I'm missing please? 下面是我的代码,有人可以告诉我我缺少什么吗?

View-Model 视图模型

 public class ClassViewModel
    {
        public int Id { get; set; }
        [Required(ErrorMessage="Name is required")]
        public string Name { get; set; }
        [Required(ErrorMessage = "Abbreviation is required")]
        public string Abbreviation { get; set; }
        [Required(ErrorMessage = "Description is required")]
        public string Description { get; set; }
        public string DescriptionFrench { get; set; }
        [Required(ErrorMessage = "Semesters is required")]
        public string Semesters { get; set; }
        [Required(ErrorMessage = "Year is required")]
        public string Year { get; set; }
        [Required(ErrorMessage = "Date is required")]
        public string Date { get; set; }
        [Required(ErrorMessage = "Time is required")]
        public string Time { get; set; }
        public bool French { get; set; }
        public bool Shared { get; set; }
        public bool Concentration { get; set; }
        public IEnumerable<DepartmentViewModel> Departments { get; set; }
    }

Controller Action 控制器动作

 public ActionResult Index()
    {            
        IEnumerable<ClassViewModel> classes = db.classes.AsEnumerable().ToList()
            .Select(c => new ClassViewModel()
            {
                Id                = c.CID,
                Name              = c.Classname,
                Abbreviation      = c.Abbreviation,
                Description       = c.Description,
                DescriptionFrench = c.DescriptionFR,
                Semesters         = c.Semesters,
                Year              = c.Year,
                Date              = c.DateTime.ToShortDateString(),
                Time              = c.DateTime.ToShortTimeString(),
                French            = c.French,
                Shared            = c.shared,
                Concentration     = c.Concentration
            });
        return View(classes);
    }

Layout file contains a Scripts section with the following 布局文件包含带有以下内容的“脚本”部分

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/Scripts/knockout-2.1.0.js")
@Scripts.Render("~/Scripts/jquery.dataTables.js")
@Scripts.Render("~/content/themes/bootstrap/js/bootstrap.js")
@Scripts.Render("~/Scripts/hideshow.js")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/fullcalendar.min.js")    
@Scripts.Render("~/Content/themes/admin/js/changePassword.js")
@RenderSection("scripts", required: false)

View 视图

@model IEnumerable<IAUCollege.Models.ClassViewModel>

@{
    ViewBag.Title = "Classes";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<h2>Classes</h2>
<p>
    Type the class name, abbreviation and/or semester to filter the results.
</p>
<div class="searchbox-container">
    @Html.ActionLink("Add Class", "Create", new {}, new {@class="btn btn-primary pull-right"})
    <input type="text" class="searchbox pull-right" id="classSearch" placeholder="Search Classes"/>
</div>  
<table id="classesTable" class="table white-table table-striped table-bordered table-hover">
    <tr>    
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Abbreviation)
        </th>     
        <th>
            @Html.DisplayNameFor(model => model.Semesters)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>        
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Abbreviation)
            </td>          
            <td>
                @Html.DisplayFor(modelItem => item.Semesters)
            </td>        
            <td>
                <div class="btn-group">
                    <a href="/admin/classes/edit/@item.Id" rel="tooltip" title="Edit"><i class="icon-edit"></i></a>
                    <a href="/admin/classes/details/@item.Id" rel="tooltip" title="Details"><i class="icon-list-alt"></i></a>
                    <a href="/admin/classes/delete/@item.Id" rel="tooltip" title="Delete"><i class="icon-remove"></i></a>
                </div>
            </td>
        </tr>
    }

</table>
@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#classesTable").dataTable();
        });
    </script>
}

When I view the page I get the following javascript error. 当我查看页面时,我收到以下javascript错误。 Uncaught TypeError: Cannot read property 'asSorting' of undefined 未捕获的TypeError:无法读取未定义的属性'asSorting'

Please tell me what I'm missing to get this working...I think it's something to do with the buttons column, but I'm not sure. 请告诉我我缺少什么让这个工作...我认为它与按钮列有关,但我不确定。 Thanks. 谢谢。

OK. 好。 So I figured it out.. I needed to have a <thead> and <tbody> tag, because tables need to be properly formatted, in-order for dataTables to work. 所以我想出来了..我需要一个<thead><tbody>标签,因为需要对表格进行适当格式化,以便数据表能够正常工作。

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

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