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

效果图

 

 

实现过程 

控制器代码

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 代码

//------------------------------------------------------------------------------
// <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

  public static bool AddGood(Goods good)
  {

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

      return db.SaveChanges() > 0;

  }

 BLL

 public static bool AddGood(Goods good)
 {
     return DAL.GoodsService.AddGood(good);
 }

Index 

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

ADD

@{
    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. TCP协议是安全的吗?

    2024-04-23 07:48:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-23 07:48:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-23 07:48:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-23 07:48:04       20 阅读

热门阅读

  1. Kubernetes 核心技术组件Scheduler解析

    2024-04-23 07:48:04       51 阅读
  2. I fumo 星(STL,数学)

    2024-04-23 07:48:04       18 阅读
  3. Nginx四层负载均衡

    2024-04-23 07:48:04       26 阅读
  4. CSS3 transition过渡:打造流畅动画效果的全面指南

    2024-04-23 07:48:04       34 阅读
  5. 天星金融消保课堂开讲,金融健康意识再提升

    2024-04-23 07:48:04       16 阅读
  6. 说说redis的集群的原理吧

    2024-04-23 07:48:04       15 阅读
  7. redis 无占用 两种方式 清除大批量数据 lua脚本

    2024-04-23 07:48:04       13 阅读
  8. gitlab上传新创建的工程项目

    2024-04-23 07:48:04       60 阅读
  9. MySQL-数据目录

    2024-04-23 07:48:04       45 阅读
  10. 2007. 从双倍数组中还原原数组

    2024-04-23 07:48:04       17 阅读
  11. 微服务(学习)

    2024-04-23 07:48:04       16 阅读
  12. 介绍下volatile关键字

    2024-04-23 07:48:04       13 阅读