<1>
Home控制器
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplication4.Models; namespace MvcApplication4.Controllers { public class HomeController : Controller { private salesEntities db = new salesEntities();//salesEntities是一个ADO.NET实体数据模型类 // // GET: /Home/ public ActionResult Index() { return View(db.T_UserInfo.ToList()); } // // GET: /Home/Details/5 public ActionResult Details(int id = 0) { T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id); if (t_userinfo == null) { return HttpNotFound(); } return View(t_userinfo); } // // GET: /Home/Create public ActionResult Create() { return View(); } // // POST: /Home/Create [HttpPost] public ActionResult Create(T_UserInfo t_userinfo) { if (ModelState.IsValid) { db.T_UserInfo.AddObject(t_userinfo); db.SaveChanges(); return RedirectToAction("Index"); } return View(t_userinfo); } // // GET: /Home/Edit/5 public ActionResult Edit(int id = 0) { T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id); if (t_userinfo == null) { return HttpNotFound(); } return View(t_userinfo); } // // POST: /Home/Edit/5 [HttpPost] public ActionResult Edit(T_UserInfo t_userinfo) { if (ModelState.IsValid) { db.T_UserInfo.Attach(t_userinfo); db.ObjectStateManager.ChangeObjectState(t_userinfo, EntityState.Modified); db.SaveChanges(); return RedirectToAction("Index"); } return View(t_userinfo); } // // GET: /Home/Delete/5 public ActionResult Delete(int id = 0) { T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id); if (t_userinfo == null) { return HttpNotFound(); } return View(t_userinfo); } // // POST: /Home/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { T_UserInfo t_userinfo = db.T_UserInfo.Single(t => t.id == id); db.T_UserInfo.DeleteObject(t_userinfo); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }
Index 视图 (这是首页)
@model IEnumerable<MvcApplication4.Models.T_UserInfo> @{ ViewBag.Title = "Index"; } <h2>首页</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> @* @Html.DisplayNameFor(model => model.Name)*@ @Html.Label("姓名") </th> <th> @* @Html.DisplayNameFor(model => model.Age)*@ @Html.Label("年龄") </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Age) </td> <td> @Html.ActionLink("编辑", "Edit", new { id=item.id }) | @Html.ActionLink("明细", "Details", new { id=item.id }) | @Html.ActionLink("删除", "Delete", new { id=item.id }) </td> </tr> } </table>
Create视图
@model MvcApplication4.Models.T_UserInfo @{ ViewBag.Title = "Create"; } <h2>创建</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>T_UserInfo</legend> <div class="editor-label"> @Html.LabelFor(model => model.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div class="editor-label"> @Html.LabelFor(model => model.Age) </div> <div class="editor-field"> @Html.EditorFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age) </div> <p> <input type="submit" value="确定添加" /> </p> </fieldset> } <div> @Html.ActionLink("跳转到首页", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
delete 视图
@model MvcApplication4.Models.T_UserInfo @{ ViewBag.Title = "Delete"; } <h2>删除</h2> <h3>你确定要删除它吗??</h3> <fieldset> <legend>T_UserInfo</legend> <table> <tr><th>@Html.Label("姓名:")</th><td>@Html.DisplayFor(model => model.Name)</td></tr> <tr><th>@Html.Label("年龄:")</th><td> @Html.DisplayFor(model => model.Age)</td></tr> </table> </fieldset> @using (Html.BeginForm()) { <p> <input type="submit" value="确定删除" /> | @Html.ActionLink("跳转到首页", "Index") </p> }
Details视图
@model MvcApplication4.Models.T_UserInfo @{ ViewBag.Title = "Details"; } <h2>明细</h2> <fieldset> <legend>T_UserInfo</legend> <table> <tr><th>@Html.Label("姓名:")</th><td>@Html.DisplayFor(model => model.Name)</td></tr> <tr><th>@Html.Label("年龄:")</th><td> @Html.DisplayFor(model => model.Age)</td></tr> </table> </fieldset> <p> @Html.ActionLink("编辑", "Edit", new { id=Model.id }) | @Html.ActionLink("跳转到首页", "Index") </p>
Edit 视图
@model MvcApplication4.Models.T_UserInfo @{ ViewBag.Title = "Edit"; } <h2>编辑</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>T_UserInfo</legend> @Html.HiddenFor(model => model.id) <div class="editor-label"> @* @Html.LabelFor(model => model.Name)*@ @Html.Label("姓名") </div> <div class="editor-field"> @Html.EditorFor(model => model.Name) @Html.ValidationMessageFor(model => model.Name) </div> <div class="editor-label"> @*@Html.LabelFor(model => model.Age)*@ @Html.Label("年龄") </div> <div class="editor-field"> @Html.EditorFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age) </div> <p> <input type="submit" value="保存" /> </p> </fieldset> } <div> @Html.ActionLink("跳转到首页", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
时间: 2024-11-09 00:29:43