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

效果图

实现过程

控制器代码

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index(bool PriceOrderDese=false)
        {
        ViewData["goods"] = BLL.GoodsManger.Show();
       ViewData["sum"] = BLL.GoodsManger.Sun();

       ViewData["goods"] = BLL.GoodsManger.getGoodlist(PriceOrderDese);
       ViewBag.PriceOrderDese = !PriceOrderDese;

       return View();
        }
        public ActionResult DeleteGood(string goodId) {
            BLL.GoodsManger.Delect(goodId);

            return RedirectToAction("Index");
        }
        public ActionResult AddGood()
        {
            return View();

        }
        [HttpPost]
        public ActionResult AddGood(Goods good)
        {
            //获取表单验证状态
            if (ModelState.IsValid)
            {
                if (BLL.GoodsManger.AddGood(good))
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            else
            {

                ViewBag.Good = good;
                return View();
            }



           

        }

    }
}

models 代码

cs 复制代码
//------------------------------------------------------------------------------
// <auto-generated>
//    此代码是根据模板生成的。
//
//    手动更改此文件可能会导致应用程序中发生异常行为。
//    如果重新生成代码,则将覆盖对此文件的手动更改。
// </auto-generated>
//------------------------------------------------------------------------------

namespace MvcApplication1.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    public partial class Goods
    {
        public int GoodsID { get; set; }
        [Required(ErrorMessage = "商品名称是必填项")]//必填
        [StringLength(10, ErrorMessage = "长度错误")]//长度限制
        public string GoodsName { get; set; }

        [Required(ErrorMessage = "商品价格是必填项")]//必填  
        public decimal GoodsPrice { get; set; }

        [Required(ErrorMessage = "商品库存是必填项")]//必填  
        public int GoodsStock { get; set; }
    }
}

DAL

cs 复制代码
  public static bool AddGood(Goods good)
  {

      GoodsDBEntities db = new GoodsDBEntities();
      db.Goods.Add(good);

      return db.SaveChanges() > 0;

  }

BLL

cs 复制代码
 public static bool AddGood(Goods good)
 {
     return DAL.GoodsService.AddGood(good);
 }

Index

cs 复制代码
 @Html.ActionLink("添加", "AddGood")

ADD

html 复制代码
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AddGood</title>
</head>
<body>
     <div>
        <form action="/Home/AddGood" method="post">
            商品名称<input type="text" name="GoodsName" value="@(ViewBag.Good!=null? ViewBag.Good.GoodsName:"")" />  @Html.ValidationMessage("GoodsName")<br />
            商品价格<input type="text" name="GoodsPrice"   />   @Html.ValidationMessage("GoodsPrice")<br />
            商品库存<input type="text" name="GoodsStock"   />@Html.ValidationMessage("GoodsStock")<br />


            <input type="submit" value="保存" />
            @Html.ValidationSummary()


        </form>
    </div>
</body>
</html>
相关推荐
爱海贼的无处不在1 分钟前
一个需求竟然要开14个会:程序员的日常到底有多“会”?
后端·程序员
IT_陈寒1 小时前
Java 性能优化:5个被低估的JVM参数让你的应用吞吐量提升50%
前端·人工智能·后端
南囝coding1 小时前
《独立开发者精选工具》第 018 期
前端·后端
绝无仅有1 小时前
数据库MySQL 面试之死锁与排查经验总结
后端·面试·github
用户384958730692 小时前
Spring Boot 集成 Redis 的完整流程
后端
昨日的风2 小时前
springboot 多数据源切换
后端
绝无仅有2 小时前
mysql性能优化实战与总结
后端·面试·github
用户8356290780512 小时前
从手动编辑到代码生成:Python 助你高效创建 Word 文档
后端·python
德育处主任3 小时前
玩转 Strands:AI Agent 开发,原来可以这么简单!
后端·aigc
Undoom3 小时前
大模型选型“炼狱”与终结:一份来自普通开发者的AI Ping深度评测报告
后端