当前位置: 首页 > news >正文

MVC基础——市场管理系统(一)


文章目录

  • 项目地址
  • 一、创建项目结构
    • 1.1 创建程序以及Controller
    • 1.2 创建View
    • 1.3 创建Models层,并且在Edit页面显示
    • 1.4 创建Layou模板页面
    • 1.5 创建静态文件css中间件
  • 二、Categories的CRUD
    • 2.1 使用静态仓库存储数据
    • 2.2 将Categorie的列表显示在页面中(List)
    • 2.3 创建_ViewImport.cs文件,将有公共引入写入
    • 2.4 创建Edit页面的表单提交 (Update)
      • 2.4.1 给修改字段添加一些验证
      • 2.4.2 使用数据注解Data Annotations
    • 2.5 添加Add逻辑(Create)
      • 2.5.1 在Controller里添加Add的逻辑
      • 2.5.2 在Index页面添加Add按钮
    • 2.6 删除逻辑
      • 2.6.1 在Layout页面设置一个Js的异步Section
    • 2.7 使用Partial View处理重复代码
      • 2.7.1 使用ViewBag传递Controller的名称
    • 2.7.2 提取公共部分,不同的地方进行判断
    • 2.7.3 删除重复代码,使用partial视图
  • ViewModel的设计原则


项目地址

  • 教程作者:王教员

  • 教程地址:

https://www.bilibili.com/video/BV1Sn4y1o7EV?spm_id_from=333.788.player.switch&vd_source=791e6deaa9c8a56b1f845a0bc1431b71&p=6
  • 代码仓库地址:
https://github.com/CXTV/WebApp
  • 所用到的框架和插件:
.net 8

一、创建项目结构

1.1 创建程序以及Controller

  • 使用空的.net core程序来制作mvc结构
  1. 创建一个空的.net core程序
  2. 在Program.cs的程序入口,注册我们依赖项
var builder = WebApplication.CreateBuilder(args);//1.注入服务
builder.Services.AddControllersWithViews();var app = builder.Build();//2.添加中间件
app.UseRouting();//3.添加控制器
app.MapControllerRoute(name: "default",pattern: "{controller=Home}/{action=Index}/{id?}");app.Run();
  1. 创建controllers文件夹,并且添加一个HomeController.cs
  • 当一个url通过匹配后,先回找到对应的controller,然后根据页面的view,找到对应的action; 例如:用户访问了/Home/Index.html,就回找到下面的方法Index()
using Microsoft.AspNetCore.Mvc;namespace WebApp.Controllers
{public class HomeController : Controller{public string Index(){return "Hello /Home/index";}}
}
  1. 通过访问https://localhost:7140/home/index就可以访问到返回的字符串

1.2 创建View

  1. 直接在HomeController里的Index() Action自动添加View,就会得到下面的文件结构,其中Home对应的就是HomeController控制器;Index.cshtml就是对应的action的视图文件
    在这里插入图片描述

  2. 将需要编辑的内容放入到视图页面/Views/Home/Index.cshtml
    在这里插入图片描述

1.3 创建Models层,并且在Edit页面显示

  1. 创建Models文件夹,并且添加Category.cs
namespace WebApp.Models
{public class Category{public int CategoryId { get; set; }public string? Name { get; set; }public string? Description { get; set; }}
}
  1. CategoriesController.cs里添加一个Edit页面,实例化Models里的类,并且传递给View()视图
public IActionResult Edit(int? id)
{var category = new Category{CategoryId = id.HasValue?id.Value : 0};return View(category);
}
  1. 创建一个Eidt的视图, 将Index页面传递的Id,展示在Edit页面里

在这里插入图片描述

1.4 创建Layou模板页面

  1. Views文件夹里Shared文件夹,并且创建_Layout.cshtml

在这里插入图片描述
2. 添加/View/_ViewStart.cshtml,全局的页面都将使用_Layout.cshtml页面作为模板页

@{Layout = "_Layout";
}
  1. 补充,如果其他页面想用其他的Layout,可以使用
Views/
├── _ViewStart.cshtml  // 全局的默认配置
├── Home/
│   ├── Index.cshtml
│   ├── _ViewStart.cshtml  // 只影响 Home 下的视图
  1. 其他页面只需要写@RenderBody()里面的内容即可
<h3 class="d-block">Categories</h3>
<div class="d-block"><ul><li><a href="/categories/edit/1">Beverage</a></li><li><a href="/categories/edit/2">Meet</a></li></ul>
</div>

1.5 创建静态文件css中间件

  1. 添加ww
http://www.lryc.cn/news/501339.html

相关文章:

  • 使用docker-compose安装Milvus向量数据库及Attu可视化连接工具
  • PostgreSQL函数中使用now()或current_timestamp的异同
  • 开发类似的同款小程序系统制作流程
  • bsp是板级支持包
  • P1784 数独 C语言(普遍超时写法)
  • 基于最新的Apache StreamPark搭建指南
  • 思科模拟器路由器的基本配置
  • vue3 computed watch 拓展reduce函数
  • MyBatis 中 SQL 片段复用
  • 【实操GPT-SoVits】声音克隆模型图文版教程
  • 用HTML和CSS实现3D圣诞树效果
  • Burp入门(10)-IP伪造插件
  • Mac软件推荐
  • 实验14 RNN的记忆能力和梯度爆炸实验
  • LeetCode面试题04 检查平衡性
  • oracle归档模式下的快速热备方法-适合小库
  • 【机器学习】【分子属性预测】——python读取.tar.gz文件(以OC22数据集为例)
  • Qt中禁止或管理任务栏关闭窗口的行为
  • docker的网络类型和使用方式
  • 二维立柱图|积水类问题
  • vue前端实现导出页面为word(两种方法)
  • 22. Three.js案例-创建旋转的圆环面
  • Elasticsearch:使用阿里 infererence API 及 semantic text 进行向量搜索
  • Linux WEB服务器的部署及优化
  • 人工智能大模型LLM开源资源汇总(持续更新)
  • 目标跟踪算法:SORT、卡尔曼滤波、匈牙利算法
  • Java版-图论-拓扑排序与有向无环图
  • GTC2024 回顾 | 优阅达携手 HubSpot 亮相上海,赋能企业数字营销与全球业务增长
  • eclipse启动的时候,之前一切很正常,但突然报Reason: Failed to determine a suitable driver class的解决
  • _tkinter.TclError: can‘t find package tkdnd Unable to load tkdnd library.解决办法