【发布时间】: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