关于MVC/P 的简单介绍

写在最前面的那些话

相信对于大多数小白来说,关于MVPMVC设计模式肯定是听过也看到过很多次了,也许也有过一些简单了解,但关于TA的具体概念,如何使用以及具体应用等都毫无所知,所以本着许多小伙伴一看到mvpmvc就一脸懵逼的表情(当然也包括本人了⊙▽⊙)#),最近上手一个基于mvp框架的mosby框架的练手项目,所以决定
去好好研究一番mvp设计模式以及mosby的原理以及使用,所以在此写下这篇博文,希望能够帮助到正在学习或者准备学习mvp设计模式的那些小伙伴了.
相信很多小伙伴应该都有这样一个共同的认知,关于技术,网上一搜一大堆,但是质量自然是参差不齐,身处互联网时代最好的一点就是资源共享,本着为更多的小伙伴节省搜寻资源的时间以及筛选优质资源的时间成本,决定以小白的身份来写写mvp那些事,旨在共享资源,共同进步,不喜勿喷!

快加入学习的队伍来吧,欢迎各路大神留下宝贵意见.

什么是MVC(Model View Controller)模式?
mvc模式

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是用MVC实现简单的增删改查的步骤: 1. 创建一个Model类,用于定义数据结构和操作数据库的方法。例如: ``` public class User { public int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public string Password { get; set; } } public class UserModel { public List<User> GetAllUsers() { // 查询数据库中的所有用户 } public User GetUserById(int id) { // 根据ID查询用户 } public void AddUser(User user) { // 添加用户到数据库 } public void UpdateUser(int id, User user) { // 更新指定ID的用户信息 } public void DeleteUser(int id) { // 删除指定ID的用户 } } ``` 2. 创建一个Controller类,用于处理用户的请求,调用Model类的方法来操作数据库,并返回视图。例如: ``` public class UserController : Controller { private UserModel userModel = new UserModel(); public ActionResult Index() { List<User> userList = userModel.GetAllUsers(); return View(userList); } public ActionResult Details(int id) { User user = userModel.GetUserById(id); return View(user); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(User user) { if (ModelState.IsValid) { userModel.AddUser(user); return RedirectToAction("Index"); } return View(user); } public ActionResult Edit(int id) { User user = userModel.GetUserById(id); return View(user); } [HttpPost] public ActionResult Edit(int id, User user) { if (ModelState.IsValid) { userModel.UpdateUser(id, user); return RedirectToAction("Index"); } return View(user); } public ActionResult Delete(int id) { User user = userModel.GetUserById(id); return View(user); } [HttpPost] public ActionResult Delete(int id, FormCollection collection) { userModel.DeleteUser(id); return RedirectToAction("Index"); } } ``` 3. 创建视图文件(.cshtml)来显示数据和处理用户输入。例如: Index.cshtml: ``` @model List<User> <table> <tr> <th>ID</th> <th>Name</th> <th>Email</th> <th>Password</th> <th>Actions</th> </tr> @foreach (var user in Model) { <tr> <td>@user.Id</td> <td>@user.Name</td> <td>@user.Email</td> <td>@user.Password</td> <td> @Html.ActionLink("Details", "Details", new { id = user.Id }) | @Html.ActionLink("Edit", "Edit", new { id = user.Id }) | @Html.ActionLink("Delete", "Delete", new { id = user.Id }) </td> </tr> } </table> @Html.ActionLink("Create New User", "Create") ``` Details.cshtml: ``` @model User <h2>@Model.Name</h2> <p>Email: @Model.Email</p> <p>Password: @Model.Password</p> @Html.ActionLink("Edit", "Edit", new { id = Model.Id }) | @Html.ActionLink("Delete", "Delete", new { id = Model.Id }) ``` Create.cshtml: ``` @model User @using (Html.BeginForm()) { @Html.ValidationSummary(true) <div> @Html.LabelFor(model => model.Name) @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div> @Html.LabelFor(model => model.Email) @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) </div> <div> @Html.LabelFor(model => model.Password) @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <p> <input type="submit" value="Create" /> </p> } @Html.ActionLink("Back to List", "Index") ``` Edit.cshtml: ``` @model User @using (Html.BeginForm()) { @Html.ValidationSummary(true) @Html.HiddenFor(model => model.Id) <div> @Html.LabelFor(model => model.Name) @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div> @Html.LabelFor(model => model.Email) @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) </div> <div> @Html.LabelFor(model => model.Password) @Html.EditorFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <p> <input type="submit" value="Save" /> </p> } @Html.ActionLink("Back to List", "Index") ``` Delete.cshtml: ``` @model User <h2>Are you sure you want to delete this user?</h2> <p>@Model.Name</p> @using (Html.BeginForm()) { <input type="submit" value="Delete" /> } @Html.ActionLink("Cancel", "Index") ``` 这样,我们就用MVC实现了一个简单的增删改查功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值