• ASP.NET MVC企业级程序设计 (接上个作品加了添加)


    效果图

     

     

    实现过程 

    控制器代码

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using System.Web;
    5. using System.Web.Mvc;
    6. using MvcApplication1.Models;
    7. namespace MvcApplication1.Controllers
    8. {
    9. public class HomeController : Controller
    10. {
    11. //
    12. // GET: /Home/
    13. public ActionResult Index(bool PriceOrderDese=false)
    14. {
    15. ViewData["goods"] = BLL.GoodsManger.Show();
    16. ViewData["sum"] = BLL.GoodsManger.Sun();
    17. ViewData["goods"] = BLL.GoodsManger.getGoodlist(PriceOrderDese);
    18. ViewBag.PriceOrderDese = !PriceOrderDese;
    19. return View();
    20. }
    21. public ActionResult DeleteGood(string goodId) {
    22. BLL.GoodsManger.Delect(goodId);
    23. return RedirectToAction("Index");
    24. }
    25. public ActionResult AddGood()
    26. {
    27. return View();
    28. }
    29. [HttpPost]
    30. public ActionResult AddGood(Goods good)
    31. {
    32. //获取表单验证状态
    33. if (ModelState.IsValid)
    34. {
    35. if (BLL.GoodsManger.AddGood(good))
    36. {
    37. return RedirectToAction("Index");
    38. }
    39. else
    40. {
    41. return View();
    42. }
    43. }
    44. else
    45. {
    46. ViewBag.Good = good;
    47. return View();
    48. }
    49. }
    50. }
    51. }

    models 代码

    1. //------------------------------------------------------------------------------
    2. //
    3. // 此代码是根据模板生成的。
    4. //
    5. // 手动更改此文件可能会导致应用程序中发生异常行为。
    6. // 如果重新生成代码,则将覆盖对此文件的手动更改。
    7. //
    8. //------------------------------------------------------------------------------
    9. namespace MvcApplication1.Models
    10. {
    11. using System;
    12. using System.Collections.Generic;
    13. using System.ComponentModel.DataAnnotations;
    14. public partial class Goods
    15. {
    16. public int GoodsID { get; set; }
    17. [Required(ErrorMessage = "商品名称是必填项")]//必填
    18. [StringLength(10, ErrorMessage = "长度错误")]//长度限制
    19. public string GoodsName { get; set; }
    20. [Required(ErrorMessage = "商品价格是必填项")]//必填
    21. public decimal GoodsPrice { get; set; }
    22. [Required(ErrorMessage = "商品库存是必填项")]//必填
    23. public int GoodsStock { get; set; }
    24. }
    25. }

    DAL

    1. public static bool AddGood(Goods good)
    2. {
    3. GoodsDBEntities db = new GoodsDBEntities();
    4. db.Goods.Add(good);
    5. return db.SaveChanges() > 0;
    6. }

     BLL

    1. public static bool AddGood(Goods good)
    2. {
    3. return DAL.GoodsService.AddGood(good);
    4. }

    Index 

     @Html.ActionLink("添加", "AddGood")

    ADD

    1. @{
    2. Layout = null;
    3. }
    4. html>
    5. <html>
    6. <head>
    7. <meta name="viewport" content="width=device-width" />
    8. <title>AddGoodtitle>
    9. head>
    10. <body>
    11. <div>
    12. <form action="/Home/AddGood" method="post">
    13. 商品名称<input type="text" name="GoodsName" value="@(ViewBag.Good!=null? ViewBag.Good.GoodsName:"")" /> @Html.ValidationMessage("GoodsName")<br />
    14. 商品价格<input type="text" name="GoodsPrice" /> @Html.ValidationMessage("GoodsPrice")<br />
    15. 商品库存<input type="text" name="GoodsStock" />@Html.ValidationMessage("GoodsStock")<br />
    16. <input type="submit" value="保存" />
    17. @Html.ValidationSummary()
    18. form>
    19. div>
    20. body>
    21. html>

     

     

  • 相关阅读:
    【软件设计】DAO分层设计练习
    「点燃我,温暖你」用Python制作一个动态爱心效果
    IntelliJ IDEA 打开近期工作的项目的对话框的快捷键
    Python: 数据类型转换总结(list-np.array-torch.tensor)
    MySQL提示sql_mode=only_full_group_by解决办法
    Python爬虫批量下载图片
    opencv 训练
    分布式事务之BASE理论
    超详细!一步到位!MySQL安装和基本使用
    NSSCTF-Web题目18(反序列化)
  • 原文地址:https://blog.csdn.net/Mr_wangzu/article/details/137975108