diff --git "a/\351\231\210\346\230\237\346\234\210/20241205\350\241\250\345\215\225.md" "b/\351\231\210\346\230\237\346\234\210/20241205\350\241\250\345\215\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..cd3eddd8f98b061418a2b66fea44ee12f8a528ee --- /dev/null +++ "b/\351\231\210\346\230\237\346\234\210/20241205\350\241\250\345\215\225.md" @@ -0,0 +1,175 @@ +搭建一个经典的CRUD(创建、读取、更新、删除)界面使用ASP.NET MVC框架,可以分为以下几个步骤: + +## 1. 创建项目 +首先,你需要创建一个新的ASP.NET MVC项目。如果你使用的是Visual Studio,可以通过以下步骤创建: + ++ 打开Visual Studio。 ++ 选择“创建新项目”。 ++ 选择“ASP.NET Web应用程序”。 ++ 在模板选择页面,选择“MVC”模板。 ++ 点击“创建”,然后命名你的项目。 +## 2. 定义模型 +创建模型(Model),这将是CRUD操作的数据结构。例如,如果你正在创建一个用户管理系统,你的模型可能如下: +```css +public class User +{ + public int Id { get; set; } + public string Name { get; set; } + public string Email { get; set; } + // 其他属性... +} +``` +## 3. 创建数据库上下文 +创建一个继承自DbContext的类,用于与数据库交互: +```css +public class ApplicationDbContext : DbContext +{ + public ApplicationDbContext() : base("DefaultConnection") { } + + public DbSet Users { get; set; } +} +``` +## 4. 配置数据库 +在Web.config文件中配置数据库连接字符串: +```css + + + +``` +## 5. 创建控制器 +创建一个控制器(Controller)来处理CRUD操作。例如: +```css +public class UsersController : Controller +{ + private ApplicationDbContext db = new ApplicationDbContext(); + + // GET: Users + public ActionResult Index() + { + return View(db.Users.ToList()); + } + + // GET: Users/Details/5 + public ActionResult Details(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + User user = db.Users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // GET: Users/Create + public ActionResult Create() + { + return View(); + } + + // POST: Users/Create + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Create([Bind(Include = "Id,Name,Email")] User user) + { + if (ModelState.IsValid) + { + db.Users.Add(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(user); + } + + // GET: Users/Edit/5 + public ActionResult Edit(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + User user = db.Users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: Users/Edit/5 + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult Edit([Bind(Include = "Id,Name,Email")] User user) + { + if (ModelState.IsValid) + { + db.Entry(user).State = EntityState.Modified; + db.SaveChanges(); + return RedirectToAction("Index"); + } + return View(user); + } + + // GET: Users/Delete/5 + public ActionResult Delete(int? id) + { + if (id == null) + { + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + } + User user = db.Users.Find(id); + if (user == null) + { + return HttpNotFound(); + } + return View(user); + } + + // POST: Users/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public ActionResult DeleteConfirmed(int id) + { + User user = db.Users.Find(id); + db.Users.Remove(user); + db.SaveChanges(); + return RedirectToAction("Index"); + } +} +``` +## 6. 创建视图 +为每个操作创建视图(View)。例如,Index视图用于显示所有用户,Create视图用于添加新用户,等等。 +```css + +@model IEnumerable + + + + + + + + @foreach (var user in Model) + { + + + + + + } +
NameEmail
@user.Name@user.Email + @Html.ActionLink("Edit", "Edit", new { id = user.Id }) | + @Html.ActionLink("Details", "Details", new { id = user.Id }) | + @Html.ActionLink("Delete", "Delete", new { id = user.Id }) +
+``` +## 7. 运行和测试 +运行你的应用程序并测试CRUD功能是否正常工作。 + +这是一个基本的CRUD界面搭建流程。根据你的具体需求,你可能需要添加更多的功能,如验证、授权、更复杂的查询等。 +1、 +![alt text](image.png) +2、 diff --git "a/\351\231\210\346\230\237\346\234\210/image.png" "b/\351\231\210\346\230\237\346\234\210/image.png" new file mode 100644 index 0000000000000000000000000000000000000000..67710540b9fc9a889181d482e6d613b79bab2437 Binary files /dev/null and "b/\351\231\210\346\230\237\346\234\210/image.png" differ