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.Employeesselectnew SelectListItem{Text = p.Dept,Value = p.Dept}).DistinctBy(p => p.Text);// GET: Defaultpublic 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);}

效果与第一种方式相同。

ASP.NET MVC3 查询解决方案相关推荐

  1. asp.net mvc3.0安装失败之终极解决方案

    安装失败截图 原因分析 因为vs10先安装了sp1补丁,然后安装的mvc3.0,某些文件被sp1补丁更改,导致"VS10-KB2483190-x86.exe"安装不了,造成安装失败 ...

  2. [转]ASP.NET MVC3 + EF 性能优化解决方案以及最优架构

    [集思广议]       我们用 asp.net mvc3 + ef 做了一个网站,现在是内测阶段,发现打开速度非常慢.首页打开(无缓存)都在5-6s以上(测试环境:程序和db都在本机),请问各位 m ...

  3. ASP.NET MVC3 + EF 性能优化解决方案以及最优架构

    [集思广议]       我们用 asp.net mvc3 + ef 做了一个网站,现在是内测阶段,发现打开速度非常慢.首页打开(无缓存)都在5-6s以上(测试环境:程序和db都在本机),请问各位 m ...

  4. ASP.NET MVC3书店--第二节 控制器(转)

    http://blog.sina.com.cn/s/blog_6ad539a90100qe6x.html 在传统的Web应用程序中,输入的URL通常被映射为一个磁盘文件.例如:一个类似"/P ...

  5. ASP.NET MVC3 快速入门

    第一节 概述    (2011-02-23 20:57:18)  转载 标签: web应用程序 分类: ASP.NETMVC3 1.1  本教程的学习内容     在本教程中,你将学会如下内容: •  ...

  6. asp.net mvc3 Razor引擎中@使用规则小记

    项目中前台用的是asp.net mvc3,Razor引擎(关于Razor的介绍可以参考:http://weblogs.asp.net/scottgu/archive/2010/07/02/introd ...

  7. [转] ASP.NET MVC3 路由和多数据集的返回

    1.ASP.NET MVC3 中的路由 同前边一样本篇并不会过多的介绍理论知识,我们在Global.asax.cs文件中可以看到如下代码: routes.MapRoute("Default& ...

  8. 实现ASP.NET MVC3 HtmlHelper 的 RadioButtonList 与CheckBoxList 扩展

    ASP.NET MVC3也出来有一段时间了,对于没有RadioButtonList 与CheckBoxList的问题,网上也已经有很多解决方案了,可以for循环拼接出来,也可以引用ASP.NET MV ...

  9. ASP.NET MVC3 快速入门--第二节 添加一个控制器

    MVC的全称为model-view-controller(模型-视图-控制器).MVC是一种开发应用程序的模式,这个模式已经具有了很好的框架架构,并且十分容易维护.使用MVC开发出来的应用程序一般包括 ...

最新文章

  1. HSV的数据结构各分量H S V的直观理解其实就是对应图片位置的的像素一一对应的矩阵表示
  2. C语言自己写得到文件大小的函数已及Python
  3. 苹果手机扩容对手机好不好?
  4. JavaScript数据类型之Boolean以及undefined和null(4)
  5. C++ 命名空间三种用法
  6. 4月12日云栖精选夜读:阿里云黄海宇:窄带高清2.0——让直播更惊艳的魔术
  7. 人工智能机器学习笔记 10月15日
  8. 个人博客系统(附源码)
  9. Yandex支持插件的手机浏览器
  10. 解决 Oracle 密码过期 the password has expired
  11. mysql 时间语句,mysql语句大全_mysql时间查询常用语句大全
  12. 《人月神话》:焦油坑
  13. HP M1136打印机 Mac驱动程序分享
  14. 成功解决raise KeyError(f“None of [{key}] are in the [{axis_name}]“)KeyError: “None of [Index([‘age.in.y
  15. Spring的全局(统一)异常处理
  16. Django 字段选项之 related_name 和 related_query_name
  17. 蓝牙音乐SRC侧的安卓实现
  18. 基于ABP和Magicodes实现Excel导出操作
  19. SpringBoot(二):详细讲解SpringBoot整合MyBatis
  20. FPGA之道(51)数据的存储

热门文章

  1. 数据分析师、大数据开发、Hadoop开发工程师、数据挖掘、算法工程师的工资薪水到底怎么样?
  2. python_绘制动态地图
  3. TokenGazer评级丨MakerDao:去中心化金融趋势确立,项目治理恐有中心化风险
  4. lm3s811 学习笔记(一)/【安装驱动/烧写程序】
  5. python小白新手所学内容(一)
  6. View/ViewGroup的生命周期
  7. OJ每日一练——爱跑步的师兄
  8. 观《风筝》电视剧感想
  9. 使用minizip解压缩多个文件(基于zlib)
  10. 开源社 2023年度理事会成员官宣