.Net商品管理(注释,百度,提问,对比,总结)

简介:

管理控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Entities;

using SportsStore.Domain.Abstract;

namespace SportsStore.WebUI.Controllers
{

    public class AdminController : Controller
    {
        private IProductRepository repository;

        public AdminController(IProductRepository repo)
        {
            repository = repo;
        }

        public ViewResult Index()
        {
            return View(repository.Products);
        }

        public ViewResult Edit(int productId)
        {
            Product product = repository.Products
                .FirstOrDefault(p => p.ProductID == productId);
            return View(product);
        }

        // 专门接受post请求处理的
        [HttpPost]
        public ActionResult Edit(Product product)
        {
            if (ModelState.IsValid) // 检测数据是否合法
            {
                repository.SaveProduct(product); // 调用保存数据的方法
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                return RedirectToAction("Index"); // 跳转到Index页面
            }
            else
            {
                return View(product);
            }
        }

        public ViewResult Create()
        {
            return View("Edit", new Product());
        }
    }
}

列表页面

@model IEnumerable<SportsStore.Domain.Entities.Product>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
}

<h2>全部商品</h2>

<p>
    @Html.ActionLink("添加一个新的产品", "Create",null,new {@class = "btn btn-default" })
</p>
<table class="table">
    <tr>
        <th>
            Id
        </th>
        <th>
            Name
        </th>
        <th>
            Price
        </th>
        <th>
            Action
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @item.ProductID
        </td>
        <td>
            @Html.ActionLink(item.Name, "Edit", new { item.ProductID })   
            @*默认就是ProductID*@
            @*超链接*@
        </td>
        <td>
            @item.Price.ToString("c")
        </td>
        <td> 
            @using (Html.BeginForm("Delete","Admin"))
            {
                @Html.Hidden("ProductId", item.ProductID)
                //创建传参的隐藏元素
                <input type="submit" class="btn btn-default btn-xs" value="Delete">
            }
            @*@Html.ActionLink("Delete", "Delete", new { id=item.ProductID })*@
        </td>
    </tr>
}

</table>

修改页面

@model SportsStore.Domain.Entities.Product
@{
    ViewBag.Title = "Admin: Edit " + @Model.Name;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    HtmlHelper.ClientValidationEnabled = false;
    HtmlHelper.UnobtrusiveJavaScriptEnabled = false;
}

<div class="panel">
    <div class="panel-heading">
        <h3>Edit @Model.Name</h3>
    </div>

    @*@using (Html.BeginForm())*@
    @*设置固定的跳转目录*@
    @using (Html.BeginForm("Edit", "Admin"))
    {
        <div class="panel-body">
            @Html.HiddenFor(m => m.ProductID)
            @foreach (var property in ViewData.ModelMetadata.Properties)
            {
                if (property.PropertyName != "ProductID")
                {
                    <div class="form-group">
                        <label>@(property.DisplayName ?? property.PropertyName)</label>
                        @if (property.PropertyName == "Description")
                        {
                            @Html.TextArea(property.PropertyName, null,
          new { @class = "form-control", rows = 5 })
                        }
                        else
                        {
                            @Html.TextBox(property.PropertyName, null,
          new { @class = "form-control" })
                        }
                         
                        @*提示错误的验证消息*@
                        @Html.ValidationMessage(property.PropertyName)

                    </div>
                }
            }
        </div>

        <div class="panel-footer">
            <input type="submit" value="Save" class="btn btn-primary" />
            @Html.ActionLink("Cancel and return to List", "Index", null, new
       {
           @class = "btn btn-default"
       })
        </div>
    }
</div>

产品模型修饰

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace SportsStore.Domain.Entities
{
    public class Product
    {
        [HiddenInput(DisplayValue = false)]
        // 隐藏起来,不显示在编辑选项中
        public int ProductID { get; set; }

        [Required(ErrorMessage = "Please enter a product name")]
        // 必填项
        public string Name { get; set; }

        [DataType(DataType.MultilineText)]
        [Required(ErrorMessage = "Please enter a description")]
        // 设置多行显示文本
        public string Description { get; set; }

        [Required]
        [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
        public decimal Price { get; set; }

        [Required(ErrorMessage = "Please specify a category")]
        public string Category { get; set; }
    }
}

接口定义修改方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Abstract
{
    public interface IProductRepository
    {
        IEnumerable<Product> Products { get; }

        // 定义保存商品的方法
        void SaveProduct(Product product);
    }
}

实现修改方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;


namespace SportsStore.Domain.Concrete
{

    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();

        public IEnumerable<Product> Products
        {
            get { return context.Products; }
        }

        // 继承接口,就要实现它的方法
        public void SaveProduct(Product product)
        {

            if (product.ProductID == 0)
            {
                context.Products.Add(product);
            }
            else
            {
                Product dbEntry = context.Products.Find(product.ProductID); // 找到商品
                if (dbEntry != null)
                {
                    dbEntry.Name = product.Name;
                    dbEntry.Description = product.Description;
                    dbEntry.Price = product.Price;
                    dbEntry.Category = product.Category;
                }
            }
            context.SaveChanges(); // 保存
        }
    }
}

422101-20170609184422262-2040092977.png

422101-20170609184426731-782669134.png

422101-20170609184431403-933619079.png

422101-20170609184436450-210868791.png

方法论:理解之后,再写个博客总结一下。不然写完博客,一样不理解。注释,百度,提问,对比,总结是一个很好的学习方法和过程。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6973315.html,如需转载请自行联系原作者

相关文章
|
XML 数据可视化 程序员
(一).NET Core WebAPI集成Swagger做接口管理
什么是Swagger? Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。 Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过 Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测。 Swagger 有什么优势? 支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需
(一).NET Core WebAPI集成Swagger做接口管理
|
8月前
|
C#
C# .net webapi使用swagger时显示controller注释
C# .net webapi使用swagger时显示controller注释
122 0
|
1月前
|
数据采集 存储 监控
.NET智慧手术室管理平台源码
术前访视记录单、手术风险评估表、手术安全核查表、自费药品或耗材、麻醉知情同意书、麻醉记录单、分娩镇痛记录单、麻醉复苏单、术后镇痛记录单、术后访视记录单、压伤风险评估量表、手术清点记录单、护理记录单、输血护理记录单。
33 0
|
1月前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
C# .net webapi使用swagger时显示controller注释
C# .net webapi使用swagger时显示controller注释
339 0
|
SQL 开发框架 前端开发
.NET&Web前端-大三-国足信息后台管理——球员管理
.NET&Web前端-大三-国足信息后台管理——球员管理
172 0
.NET&Web前端-大三-国足信息后台管理——球员管理
|
前端开发 Linux 开发工具
使用Supervisor 管理.net core mvc部署
Supervisor简介 Supervisor是一个用python编写的,linux平台下的进程守护工具,它通过子进程的方式运行目标服务。相比rc.d脚本,它能更加方便地知道子进程发生了什么,并在其崩溃时自动重启子进程。
1652 0
.NET 块注释
#region xxx #endregion
620 0
|
物联网 开发框架 数据库管理
RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本━新增岗位管理-WinForm部分
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/42420297 RDIFramework.NET ━ .NET快速信息化系统开发框架 V2.8 版本 新增岗位管理-WinForm部分     岗位(职位)管理模块主要是针对组织机构的岗位(职位)进行管理,包括:增加、修改、删除、移动、对岗位设置用户,设置岗位的权限等。
917 0