【问题标题】:How to implement datatables in asp.net mvc?如何在 asp.net mvc 中实现数据表?
【发布时间】:2016-04-12 15:45:24
【问题描述】:

所以我有一个名为 product 的实体,如下所示:

public class Product
    {
        [HiddenInput(DisplayValue=false)]
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        [DataType(DataType.MultilineText)]
        public string ProductDescription { get; set; }
        public decimal ProductPrice { get; set; }
        public string Category { get; set; }
        public string SubCategory { get; set; }
    }

我有另一个名为 EFProductRepository 的类,它派生自另一个名为 IProductRepository 的类,如下所示:

public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();
        public IEnumerable<Product> Products
        {
            get { return context.Products; }
        }
    }

然后在我的控制器操作Index 我有这个:

public ViewResult Index()
        {
            return View(repository.Products);
        }

还有一个列出表中数据的索引视图,所以问题是我如何在这段代码中实现 DataTable 客户端?我在这里尝试过jQuery Datatable with MVC 5 and Entity Framework,但不幸的是没有运气。 任何帮助表示赞赏,问候。

【问题讨论】:

  • 我假设数据表插件需要 JSON 格式的数据。将您的 context.Products 转换为 JSON 格式(可能是在将它们映射到具有插件正在寻找的属性的视图模型列表之后)
  • @Shyju 关于如何将其更改为 JSON 的任何提示?
  • 您可以使用Controller.Json 方法。

标签: jquery asp.net-mvc datatables-1.10


【解决方案1】:

如果您不想将对象转换为 JSON 并弄乱所有控制器自定义,我建议您查看我在 MVC 5 项目中使用的这个数据表。 https://gridmvc.codeplex.com/ 无论如何,这将为您提供许多您可能想要的前端功能,并且它只是直接绑定到对象列表。

文档下有简单的入门指南。

祝你好运

【讨论】:

  • 看起来是个不错的选择,我不能接受这个作为答案,但我投了赞成票。我真的需要使用 DataTables ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 2010-11-12
  • 2019-01-15
  • 1970-01-01
相关资源
最近更新 更多