VS Code + C# + ASP.NET Core MVC创建简单CRUD示例程序
以下信息转载自通义千问
🧱 技术栈
- 开发工具:Visual Studio Code
- 语言:C#
- 框架:ASP.NET Core MVC
- 数据存储:内存中模拟数据库(使用 List
)
📦 第一步:安装环境
确保你已安装以下内容:
- .NET SDK 6.0 或 7.0 或 8.0
- Visual Studio Code
- C# 插件(搜索
C# for Visual Studio Code (powered by OmniSharp))
🚀 第二步:创建项目
打开终端(PowerShell 或 bash),执行以下命令:
dotnet new mvc -n MvcCrudDemo
cd MvcCrudDemo
code .
这将创建一个 ASP.NET Core MVC 项目并用 VS Code 打开。
📁 第三步:添加模型(Model)
1. 创建模型类 Product.cs
在项目根目录下新建文件夹 Models,然后添加 Product.cs:
// Models/Product.cs
namespace MvcCrudDemo.Models
{
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
📝 第四步:创建控制器和视图
1. 创建控制器 ProductsController.cs
在 Controllers 文件夹中新建 ProductsController.cs:
// Controllers/ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using MvcCrudDemo.Models;
using System.Collections.Generic;
using System.Linq;
namespace MvcCrudDemo.Controllers
{
public class ProductsController : Controller
{
private static List<Product> _products = new List<Product>
{
new Product { Id = 1, Name = "Apple", Price = 5.99m },
new Product { Id = 2, Name = "Banana", Price = 3.49m }
};
public IActionResult Index()
{
return View(_products);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Product product)
{
product.Id = _products.Count > 0 ? _products.Max(p => p.Id) + 1 : 1;
_products.Add(product);
return RedirectToAction("Index");
}
[HttpGet]
public IActionResult Edit(int id)
{
var product = _products.FirstOrDefault(p => p.Id == id);
if (product == null) return NotFound();
return View(product);
}
[HttpPost]
public IActionResult Edit(Product product)
{
var index = _products.FindIndex(p => p.Id == product.Id);
if (index == -1) return NotFound();
_products[index] = product;
return RedirectToAction("Index");
}
[HttpGet]
public IActionResult Delete(int id)
{
var product = _products.FirstOrDefault(p => p.Id == id);
if (product == null) return NotFound();
return View(product);
}
[HttpPost]
public IActionResult Delete(int id, IFormCollection form)
{
var product = _products.FirstOrDefault(p => p.Id == id);
if (product != null)
{
_products.Remove(product);
}
return RedirectToAction("Index");
}
}
}
🖼 第五步:创建视图
在 Views/Products 下创建以下视图文件:
1. Index.cshtml
<!-- Views/Products/Index.cshtml -->
@model List<MvcCrudDemo.Models.Product>
<h2>产品列表</h2>
<p>
<a asp-action="Create">添加新产品</a>
</p>
<table class="table">
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>价格</th>
<th>操作</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Id</td>
<td>@product.Name</td>
<td>@product.Price</td>
<td>
<a asp-action="Edit" asp-route-id="@product.Id">编辑</a> |
<a asp-action="Delete" asp-route-id="@product.Id">删除</a>
</td>
</tr>
}
</tbody>
</table>
2. Create.cshtml
<!-- Views/Products/Create.cshtml -->
@model MvcCrudDemo.Models.Product
<h2>添加新产品</h2>
<form asp-action="Create">
<div>
<label>名称</label>
<input asp-for="Name" />
</div>
<div>
<label>价格</label>
<input asp-for="Price" />
</div>
<button type="submit">提交</button>
</form>
<a asp-action="Index">返回列表</a>
3. Edit.cshtml
<!-- Views/Products/Edit.cshtml -->
@model MvcCrudDemo.Models.Product
<h2>编辑产品</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="Id" />
<div>
<label>名称</label>
<input asp-for="Name" />
</div>
<div>
<label>价格</label>
<input asp-for="Price" />
</div>
<button type="submit">保存</button>
</form>
<a asp-action="Index">返回列表</a>
4. Delete.cshtml
<!-- Views/Products/Delete.cshtml -->
@model MvcCrudDemo.Models.Product
<h2>确定删除?</h2>
<p>名称: @Model.Name</p>
<p>价格: @Model.Price</p>
<form asp-action="Delete" method="post">
<input type="hidden" asp-for="Id" />
<button type="submit">删除</button>
</form>
<a asp-action="Index">取消</a>
🚀 第六步:运行项目
在终端中输入:
dotnet run
访问:http://localhost:5000/Products
你将看到一个简单的增删改查界面!
✅ 总结
你已经完成了:
- 使用 VS Code 创建了一个 ASP.NET Core MVC 项目
- 实现了一个内存中产品的 CRUD 操作
- 使用了 Razor 视图引擎渲染页面
📌 下一步建议
- 使用 Entity Framework Core 连接数据库(如 SQLite、SQL Server)
- 添加数据验证
- 使用 Bootstrap 美化界面
- 部署到 IIS 或 Linux 服务器
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/keeplearningandsharing/p/19001221
浙公网安备 33010602011771号