首页 > 技术文章 > 项目前的知识点准备(2)

mahun 2014-11-23 12:39 原文

在Controllers文件夹下添加 一个控制器 HomeController.cs。我这里之所以说的简单,并没有讲一些理念性的东西,是为了尽快让大家落实到代码上来。而且这就码字工的一个动作吧,没什么可说的。至于不知道添加的,我相信肯学的人一定能找到路子,比如你没有控制器这个概念,看到我上面一句话,你就会问度娘,c#如何添加控制器。

哈哈,增,删,改,查,明细。这个是程序员经常挂在嘴边的哦。只是页面的复杂程度不一样。
查:
  1. #region 查
  2. /// <summary>
  3. /// 查
  4. /// </summary>
  5. /// <returns></returns>
  6. public ActionResult Index()
  7. {
  8. var models = from m in db.Provinces select m;
  9. return View(models.ToList());
  10. }
  11. #endregion
这里面用了 Linq的语法,同样问度娘。Linq是.net 独有的,写linq会比普通sql难不少,和sql的学习一样,见多识广,每天都要练习,这个属于“功力”类的知识,
不要想着速成。

增:
  1. #region 增
  2. /// <summary>
  3. /// 增
  4. /// </summary>
  5. /// <returns></returns>
  6. public ActionResult Create()
  7. {
  8. return View();
  9. }
  10. /// <summary>
  11. /// 接收创建页面的数据并存储
  12. /// </summary>
  13. /// <param name="model"></param>
  14. /// <returns></returns>
  15. [HttpPost]
  16. public ActionResult Create(Province model)
  17. {
  18. //
  19. if (ModelState.IsValid)
  20. {
  21. db.Provinces.Add(model);//添加
  22. db.SaveChanges();//提交至数据库
  23. return RedirectToAction("Index");
  24. }
  25. return View(model);
  26. }
  27. #endregion
ModelState 是对实体所有属性判断验证的,通过验证 就添加并提交到数据库。然后重定向到Index页。[HttpPost]是注解。

删:
  1. #region 删
  2. /// <summary>
  3. /// 删
  4. /// </summary>
  5. /// <param name="id"></param>
  6. /// <returns></returns>
  7. public ActionResult Delete(int id)
  8. {
  9. var model = db.Provinces.Find(id);
  10. if (model == null)
  11. {
  12. return RedirectToAction("Index");
  13. }
  14. return View(model);
  15. }
  16. /// <summary>
  17. /// 删除,签名一致方法的处理
  18. /// </summary>
  19. /// <param name="id"></param>
  20. /// <param name="collection"></param>
  21. /// <returns></returns>
  22. [HttpPost]
  23. public ActionResult Delete(int id, FormCollection collection)
  24. {
  25. var model = db.Provinces.Find(id);
  26. if (model != null)
  27. {
  28. db.Provinces.Remove(model);
  29. db.SaveChanges();
  30. }
  31. return RedirectToAction("Index");
  32. }
  33. #endregion

改:
  1. #region 改
  2. /// <summary>
  3. /// 改
  4. /// </summary>
  5. /// <param name="id"></param>
  6. /// <returns></returns>
  7. public ActionResult Edit(int id)
  8. {
  9. var model = db.Provinces.Find(id);
  10. //如果为空,直接重定向到index
  11. if (model == null) return RedirectToAction("Index");
  12. return View(model);
  13. }
  14. /// <summary>
  15. /// 接收修改页面的数据并存储
  16. /// </summary>
  17. /// <param name="model"></param>
  18. /// <returns></returns>
  19. [HttpPost]
  20. public ActionResult Edit(Province model)
  21. {
  22. if (ModelState.IsValid)
  23. {
  24. //把存储状态修改成“Modified”
  25. db.Entry(model).State = System.Data.EntityState.Modified;
  26. db.SaveChanges();
  27. return RedirectToAction("Index");
  28. }
  29. return View(model);
  30. }
  31. #endregion
修改这里的实现方法,请注意。先是从后台查询一下,然后再修改,没人会批评的做法,通用的做法。有的项目里面的做法是不经过后台,而是直接把所有的值传过去。

明细:
  1. #region 明细
  2. /// <summary>
  3. /// 明细
  4. /// </summary>
  5. /// <param name="id"></param>
  6. /// <returns></returns>
  7. public ActionResult Details(int id)
  8. {
  9. var model = db.Provinces.Find(id);
  10. if (model == null)
  11. {
  12. return RedirectToAction("Index");
  13. }
  14. return View(model);
  15. }
  16. #endregion
好的,mvc和传统的tuo


来自为知笔记(Wiz)


推荐阅读