【问题标题】:How to get data-tables in asp.net to show only certain data如何在 asp.net 中获取数据表以仅显示某些数据
【发布时间】:2017-11-14 16:43:38
【问题描述】:

我不知道如何获取数据以仅显示 John 的条目。我正在使用数据表,所以不知道如何让它工作。当我启动应用程序时,它仍然显示所有用户。这是我到目前为止得到的任何帮助,我们将不胜感激。我正在尝试将 where 子句放入程序中,我是否在模型中而不是在控制器中创建它。

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Asp.NetMVCCrud.Models;

namespace Asp.NetMVCCrud.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult GetData()
        {
            using(DBModel db = new DBModel())
            {
               db.Employees.Where(Employee => Employee.Name == "John");
                List<Employee> empList = db.Employees.ToList<Employee>();
                return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
            }

        }
    }
}

员工模型生成

namespace Asp.NetMVCCrud.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Employee
    {
        public int EmployeeID { get; set; }
        public string Name { get; set; }
        public string Postion { get; set; }
        public string Office { get; set; }
        public Nullable<int> Age { get; set; }
        public Nullable<int> Salary { get; set; }
    }
}

查看索引

@{
    ViewBag.Title = "Employee List";
}

<h2>Employee Crud Operations</h2>
<table id="employeetable" class="table table-striped table-bordered">
  <thead>
      <tr>
          <th>Name</th>
          <th>Position</th>
          <th>Office</th>
          <th>Age</th>
          <th>Salary</th>
      </tr>
  </thead>
</table>
<link href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap4.min.css" rel="stylesheet" />

@section scripts{
    <script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.16/js/dataTables.bootstrap4.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#employeetable").DataTable({
                "ajax": {
                    "url": "/Employee/GetData",
                    "type": "GET",
                    "datatype":"json"

                },
                "columns": [

                    { "data": "Name" },
                      { "data": "Postion" },
                        { "data": "Office" },
                          { "data": "Age" },
                            { "data": "Salary" },

                ]

            });

        });
    </script>
    }

【问题讨论】:

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


    【解决方案1】:

    这条线db.Employees.Where(Employee =&gt; Employee.Name == "John") 目前在功能上没有做任何事情。它正在数据库中查询名为 John 的员工,然后对该数据不做任何事情。我想你想要

    List<Employee> empList = db.Employees.Where(Employee => Employee.Name == "John").ToList();
    
    return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
    

    您需要将过滤后的数据分配给一个变量,以便对其进行引用,然后将该数据作为 json 的一部分返回

    【讨论】:

      【解决方案2】:

      因为您的代码返回empList,即所有员工。

      您有一个 LINQ 查询(此行 db.Employees.Where(Employee =&gt; Employee.Name == "John");)来获取过滤后的子集,但您没有执行该查询或使用该查询的结果来返回结果。

      只返回过滤后的结果。这应该工作

      var filteredList = db.Employees.Where(e=> e.Name == "John").ToList();
      return Json(new { data = filteredList }, JsonRequestBehavior.AllowGet);
      

      【讨论】:

        猜你喜欢
        • 2010-09-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-05
        • 1970-01-01
        • 1970-01-01
        • 2016-02-01
        • 2020-06-27
        相关资源
        最近更新 更多