ASP.NET MVC 使用视图与模型绑定的方式,查询是常用的需求,今天总结了三种解决方案!
1.实体
public class Employee
{
public string EmployeeId { set; get; }
public string EmployeeName { set; get; }
public string Dept { set; get; }
public string Role { set; get; }
public string Email { set; get; }
public string Phone { set; get; }
}
2.数据
public class ViewModels
{
public static IEnumerable<Employee> Employees = new List<Employee>
{
new Employee(){ EmployeeId = "1", EmployeeName = "justin", Dept = "T", Role = "A", Email = "justin@wicresoft.com", Phone = "110"},
new Employee(){ EmployeeId = "2", EmployeeName = "justin", Dept = "T", Role = "A", Email = "justin@wicresoft.com", Phone = "110"},
new Employee(){ EmployeeId = "3", EmployeeName = "justin", Dept = "D", Role = "A", Email = "justin@wicresoft.com", Phone = "110"},
new Employee(){ EmployeeId = "4", EmployeeName = "justin", Dept = "D", Role = "A", Email = "justin@wicresoft.com", Phone = "110"},
new Employee(){ EmployeeId = "5", EmployeeName = "justin", Dept = "D", Role = "A", Email = "justin@wicresoft.com", Phone = "110"},
new Employee(){ EmployeeId = "6", EmployeeName = "justin", Dept = "F", Role = "B", Email = "justin@wicresoft.com", Phone = "110"},
};
}
3.控制器(使用formCollection)
public class DefaultController : Controller
{
private readonly IEnumerable<SelectListItem> _depts =
(
from p in ViewModels.Employees
select
new SelectListItem
{
Text = p.Dept,
Value = p.Dept
}).DistinctBy(p => p.Text);
// GET: Default
public ActionResult Index()
{
ViewData["dept"] = _depts;
return View(ViewModels.Employees);
}
[HttpPost]
public ActionResult Search(FormCollection formCollection)
{
ViewData["dept"] = _depts;
var employees = ViewModels.Employees;
string dept = formCollection["dept"];
string role = formCollection["role"];
if (!string.IsNullOrEmpty(dept))
{
employees = employees.Where(x => x.Dept == dept.Trim());
}
if (!string.IsNullOrEmpty(role))
{
employees = employees.Where(x => x.Role == role.Trim());
}
return View("Index", employees);
}
}
4.页面:
@{
Layout = null;
}
@model IEnumerable<AspNetMvcCRUD_Demo.Models.Employee>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<p>
@using (Html.BeginForm("Search", "Default", FormMethod.Post))
{
<p>
部门:@Html.DropDownList("dept", "All")
角色:@Html.TextBox("role")
<input type="submit" value="查询"/>
</p>
}
</p>
<table>
@foreach (var item in @Model)
{
<tr>
<td>@item.EmployeeId</td>
<td>@item.EmployeeName</td>
<td>@item.Dept</td>
<td>@item.Role</td>
<td>@item.Email</td>
<td>@item.Phone</td>
</tr>
}
</table>
</div>
</body>
</html>
5.效果
上面是使用FormCollection的方式传递,这样做的好处是,可以获取表单内的所有字段的信息,支持对表单的全字段搜索。通常我们不需要这样的搜索,一般是两个条件的搜索方式,不够的话再使用高级搜索方式。于是我们可以不使用FormCollection.我们需要改动控制器,页面不需要改动。
控制器需要改动search方法如下
public ActionResult Search(string dept, string role)
{
ViewData["dept"] = _depts;
var employees = ViewModels.Employees;
if (!string.IsNullOrEmpty(dept))
{
employees = employees.Where(x => x.Dept == dept.Trim());
}
if (!string.IsNullOrEmpty(role))
{
employees = employees.Where(x => x.Role == role.Trim());
}
return View("Index", employees);
}
效果与第一种方式相同。