ASP.NET MVC 4 Controller Action + View ( Razor ) + Add / Edit / Delete 方法
/App_Start/RouteConfig.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); … … routes.MapRoute( name: "Home", url: "Home/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); … … } } } |
/Home/MVCAdd ( /Views/Home/MVCAdd.cshtml ) |
@model MvcApplication2.MVC … … <table> @using (@Html.BeginForm("MVCAdd", "Home")) { <tr> <td>Name :</td> <td>@Html.TextBoxFor(m => m.MVC_Name)</td> </tr> <tr> <td>Product :</td> <td>@Html.TextBoxFor(m => m.MVC_Product)</td> </tr> <tr> <td>Description :</td> <td>@Html.TextBoxFor(m => m.MVC_Description)</td> </tr> <tr> <td colspan="2"> <input type="submit" name="Submit" value="Submit" /> </td> </tr> } </table> |
/Home/MVCUpdate ( /Views/Home/MVCUpdate.cshtml ) |
@model MvcApplication2.MVC … … <table> @using (@Html.BeginForm("MVCUpdate", "Home")) { <tr> <td>ID :</td> <td>@Html.TextBoxFor(m => m.MVC_ID, new Dictionary<string, object> {{"readonly", "readonly"}})</td> </tr> <tr> <td>Name :</td> <td>@Html.TextBoxFor(m => m.MVC_Name)</td> </tr> <tr> <td>Product :</td> <td>@Html.TextBoxFor(m => m.MVC_Product)</td> </tr> <tr> <td>Description :</td> <td>@Html.TextBoxFor(m => m.MVC_Description)</td> </tr> <tr> <td colspan="2"> <input type="submit" name="Submit" value="Submit" /> </td> </tr> } </table> |
/Home/MVCList ( /Views/Home/MVCList.cshtml ) |
@foreach (var MVC in Model) { <tr> <td><span> @Html.ActionLink("Delete", "MVCDelete", new { id = MVC.MVC_ID.ToString() }) </span></td> <td><span> @Html.ActionLink(((int)MVC.MVC_ID).ToString(), "MVCUpdate", new { id = MVC.MVC_ID.ToString() }) </span></td> <td><span> @MVC.MVC_Name </span></td> <td><span> @MVC.MVC_Product </span></td> <td><span> @MVC.MVC_Description </span></td> </tr> } </table> <div style="margin-top:10px; font-size:16px;">@Html.ActionLink("MVC Insert", "MVCAdd")</div> |
/Controller/HomeController.cs |
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcApplication.Controllers { public class HomeController : Controller { BITestEntities entity; public ActionResult MVCList() { entity = new BITestEntities(); var DataModel = entity.MVCs.ToList(); return View(DataModel); } public ActionResult MVCAdd() { return View(); } [HttpPost] public ActionResult MVCAdd(MVC Model) { if (ModelState.IsValid) { entity = new BITestEntities(); entity.MVCs.Add(Model); entity.SaveChanges(); return RedirectToAction("MVCList"); } return RedirectToAction("MVCList"); } public ActionResult MVCDelete(int? id) { if (id.HasValue) { entity = new BITestEntities(); MVC DataModel = (entity.MVCs.Where(m => m.MVC_ID == ((int)id)).FirstOrDefault()); if (DataModel != null) { entity.MVCs.Remove(DataModel); entity.SaveChanges(); return RedirectToAction("MVCList"); } else { return RedirectToAction("MVCList"); } } else { return RedirectToAction("MVCList"); } } public ActionResult MVCUpdate(int? id) { if(id.HasValue){ entity = new BITestEntities(); var DataModel = (entity.MVCs.Where(m => m.MVC_ID == ((int)id)).FirstOrDefault()); if(DataModel != null){ return View(DataModel); }else{ return RedirectToAction("MVCList"); } } else { return RedirectToAction("MVCList"); } } [HttpPost] public ActionResult MVCUpdate(int id, FormCollection formCollection) { entity = new BITestEntities(); MVC item = entity.MVCs.First(u => u.MVC_ID == id); item.MVC_Name = formCollection["MVC_Name"]; item.MVC_Product = Int32.Parse(formCollection["MVC_Product"]); item.MVC_Description = formCollection["MVC_Description"]; entity.SaveChanges(); return RedirectToAction("MVCList"); } } } |