静态网站生成,网站建设电话销售技巧和话术,企业官网建站,织梦五彩婚纱源码网_婚庆策划网站php源码文章目录 项目地址一、创建项目结构1.1 创建程序以及Controller1.2 创建View1.3 创建Models层,并且在Edit页面显示1.4 创建Layou模板页面1.5 创建静态文件css中间件二、Categories的CRUD2.1 使用静态仓库存储数据2.2 将Categorie的列表显示在页面中(List)2.3 创建_ViewImport.… 文章目录 项目地址一、创建项目结构1.1 创建程序以及Controller1.2 创建View1.3 创建Models层,并且在Edit页面显示1.4 创建Layou模板页面1.5 创建静态文件css中间件 二、Categories的CRUD2.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.switchvd_source=791e6deaa9c8a56b1f845a0bc1431b71p=6代码仓库地址:https://github.com/CXTV/WebApp所用到的框架和插件:.net 8一、创建项目结构
1.1 创建程序以及Controller
使用空的.net core程序来制作mvc结构创建一个空的.net core程序在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();
创建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";}}
}通过访问https://localhost:7140/home/index就可以访问到返回的字符串1.2 创建View 直接在HomeController里的Index() Action自动添加View,就会得到下面的文件结构,其中Home对应的就是HomeController控制器;Index.cshtml就是对应的action的视图文件 将需要编辑的内容放入到视图页面/Views/Home/Index.cshtml 1.3 创建Models层,并且在Edit页面显示
创建Models文件夹,并且添加Category.csnamespace WebApp.Models
{public class Category{public int CategoryId { get; set; }public string? Name { get; set; }public string? Description { get; set; }}
}在CategoriesController.cs里添加一个Edit页面,实例化Models里的类,并且传递给View()视图public IActionResult Edit(int? id)
{var category = new Category{CategoryId = id.HasValue?id.Value : 0};return View(category);
}创建一个Eidt的视图, 将Index页面传递的Id,展示在Edit页面里1.4 创建Layou模板页面
在Views文件夹里Shared文件夹,并且创建_Layout.cshtml2. 添加/View/_ViewStart.cshtml,全局的页面都将使用_Layout.cshtml页面作为模板页
@{Layout = "_Layout";
}补充,如果其他页面想用其他的Layout,可以使用Views/
├── _ViewStart.cshtml // 全局的默认配置
├── Home/
│ ├── Index.cshtml
│ ├── _ViewStart.cshtml // 只影响 Home 下的视图
其他页面只需要写@RenderBody()里面的内容即可h3 class="d-block"Categories/h3
div class="d-block"ullia href="/categories/edit/1"Beverage/a/lilia href="/categories/edit/2"Meet/a/li/ul
/div1.5 创建静态文件css中间件
添加ww