惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 张剑

WebMatrix&Razor建站系列之WebMatrix介绍 Windows Phone 7 XNA开发之关于游戏组件 Windows Phone 7 XNA开发之关于图形的配置 EntityFramework外健的读写 vs2010中添加项目中找不到EntityFramework实体框架解决办法 使用MSDN学习ASP.NET的工作流程 Windows Phone 7、XNA的旋转的背景 《XNA游戏开发》在战机游戏中使用Decorator模式 不被重视的基础,简单高效地使用ADO.net连接对象 微软2011 GCR MVP Open Day 之旅! ASP.NET4.5与VisualStudio11预览 程序员杂记系列文章,30岁之前的回忆。 程序员杂记:带面具的生活! MVC中在路由表routes集合中添加Route实例的一些问题。 Windows Phone 7之HelloWorld! MVC3+Entity Framework 实现投票系统(三) MVC3+Entity Framework 实现投票系统(一) 关于Windows Phone 7开发工具离线安装包 程序员杂记:我们的爱情故事
MVC3+Entity Framework 实现投票系统(二)
张剑 · 2011-11-28 · via 博客园 - 张剑

上一节,我们将Models加入了实体对象模型(Entity Frmaework模型)接下来我们要完成控制层的代码编写:

1.在Controllers(控制器)目录点右建,添加一个控制器:

2.添加Home控制器:

3.添加Admin控制器:

4.创建完成后,在Controllers目录中会增加以下两个.cs文件:

5.HomeControllers.cs中的代码如下:

  1. public class HomeController : Controller
  2. {
  3. //
  4. // GET: /Home/
  5. public ActionResult Index()
  6. {
  7. Models.VoteEntities mv = new Models.VoteEntities();//创建实体对象
  8. return View(mv.Users.ToList()); //将查询结果向视图层输出
  9. }
  10. }

6.AdminControllers.cs中代码如下:

  1. public class AdminController : Controller
  2. {
  3. //
  4. // GET: /Admin/
  5. public ActionResult Index()
  6. {
  7. Models.VoteEntities mv = new Models.VoteEntities(); //创建数据实体
  8. List<Models.Users> list = mv.Users.ToList(); //得到users表中所有信息
  9. ViewModel.List = list; //将表中信息赋值给ViewModel.List,注意List为动态表达式,是自命名的。
  10. return View();
  11. }
  12. //
  13. // GET: /Admin/Details/5
  14. public ActionResult Details(int id)
  15. {
  16. return View();
  17. }
  18. //
  19. // GET: /Admin/Create
  20. public ActionResult Create()
  21. {
  22. return View();
  23. }
  24. //
  25. // POST: /Admin/Create
  26. [HttpPost]
  27. public ActionResult Create(Models.Users mu)
  28. {
  29. try
  30. {
  31. string picname = Path.GetFileName(Request.Files["up"].FileName);//得到文件的名字
  32. string filepath = Server.MapPath("/Content/") + picname; //得到要保存在服务器上的路径
  33. Request.Files["up"].SaveAs(filepath);
  34. mu.UserPicPath = picname; //在数据加保存文件的相对路径(文件名称)就可以了
  35. Models.VoteEntities mv = new Models.VoteEntities();
  36. mv.AddToUsers(mu);
  37. mv.SaveChanges();
  38. return RedirectToAction("Index");
  39. }
  40. catch
  41. {
  42. return View();
  43. }
  44. }
  45. //
  46. // GET: /Admin/Edit/5
  47. public ActionResult Edit(int id)
  48. {
  49. return View();
  50. }
  51. //
  52. // POST: /Admin/Edit/5
  53. [HttpPost]
  54. public ActionResult Edit(int id, Models.Users mu)
  55. {
  56. try
  57. {
  58. Models.VoteEntities mv = new Models.VoteEntities();
  59. mv.Users.Single(m => m.id == id).UserName = mu.UserName; //查询出指定用户名并更新为新用户
  60. mv.Users.Single(m => m.id == id).VoteCount = mu.VoteCount; //查询出指定票数并更新为新票数
  61. mv.SaveChanges(); //保存更改
  62. return RedirectToAction("Index");
  63. }
  64. catch
  65. {
  66. return View();
  67. }
  68. }
  69. //
  70. // GET: /Admin/Delete/5
  71. public ActionResult Delete(int id)
  72. {
  73. Models.VoteEntities mv = new Models.VoteEntities();
  74. mv.DeleteObject(mv.Users.Single(m => m.id == id));//查询出指定ID的唯一值并执行删除操作
  75. mv.SaveChanges();
  76. return RedirectToAction("Index");
  77. }
  78. // POST: /Admin/Delete/5
  79. [HttpPost]
  80. public ActionResult Delete(int id, FormCollection collection)
  81. {
  82. try
  83. {
  84. return RedirectToAction("Index");
  85. }
  86. catch
  87. {
  88. return View();
  89. }
  90. }
  91. }

以上为两个控制器类中的代码,下一节,我们为控制器添加指定的视图层界面。

未完待续......