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>
相关推荐
customer081 小时前
【开源免费】基于SpringBoot+Vue.JS体育馆管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
计算机-秋大田4 小时前
基于微信小程序的电子竞技信息交流平台设计与实现(LW+源码+讲解)
spring boot·后端·微信小程序·小程序·课程设计
加油,旭杏6 小时前
【go语言】接口
开发语言·后端·golang
谢大旭6 小时前
ASP.NET Core 中间件
后端·中间件·c#
customer087 小时前
【开源免费】基于SpringBoot+Vue.JS景区民宿预约系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
uzong8 小时前
Java函数式接口:代码艺术的诗意绽放
后端
HelloDam9 小时前
基于 mzt-biz-log 实现接口调用日志记录
后端
带刺的坐椅9 小时前
无耳科技 Solon v3.0.7 发布(2025农历新年版)
java·spring·mvc·solon·aop
SomeB1oody9 小时前
【Rust自学】15.6. RefCell与内部可变性:“摆脱”安全性限制
开发语言·后端·rust