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>
相关推荐
文心快码BaiduComate4 分钟前
用Comate Zulu开发一款微信小程序
前端·后端·微信小程序
用户8356290780516 分钟前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_7 分钟前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi
ytadpole7 分钟前
Java 25 新特性 更简洁、更高效、更现代
java·后端
风一样的树懒1 小时前
死信队列:你在正确使用么?
后端
RoyLin1 小时前
TypeScript设计模式:门面模式
前端·后端·typescript
JavaGuide1 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
weiwenhao1 小时前
关于 nature 编程语言
人工智能·后端·开源
薛定谔的算法2 小时前
phoneGPT:构建专业领域的检索增强型智能问答系统
前端·数据库·后端
RoyLin2 小时前
TypeScript设计模式:责任链模式
前端·后端·typescript