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>
相关推荐
ningqw4 小时前
SpringBoot 常用跨域处理方案
java·后端·springboot
你的人类朋友4 小时前
vi编辑器命令常用操作整理(持续更新)
后端
胡gh4 小时前
简单又复杂,难道只能说一个有箭头一个没箭头?这种问题该怎么回答?
javascript·后端·面试
一只叫煤球的猫5 小时前
看到同事设计的表结构我人麻了!聊聊怎么更好去设计数据库表
后端·mysql·面试
uzong5 小时前
技术人如何对客做好沟通(上篇)
后端
颜如玉6 小时前
Redis scan高位进位加法机制浅析
redis·后端·开源
Moment6 小时前
毕业一年了,分享一下我的四个开源项目!😊😊😊
前端·后端·开源
why技术7 小时前
在我眼里,这就是天才般的算法!
后端·面试
绝无仅有7 小时前
Jenkins+docker 微服务实现自动化部署安装和部署过程
后端·面试·github
程序视点7 小时前
Escrcpy 3.0投屏控制软件使用教程:无线/有线连接+虚拟显示功能详解
前端·后端