添加Action EditUserInfo
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MvcApplicationStudy.Models; namespace MvcApplicationStudy.Controllers { public class UserInfoController : Controller { // // GET: /UserInfo/ public ActionResult Index() { TestEntities db = new TestEntities(); var userInfoList = db.UserInfo.Where<UserInfo>(c => true); List<UserInfo> list = userInfoList.ToList(); // ViewBag.Model = list; //return View(); return View(list); } //展示一条数据详细信息 public ActionResult ShowDetail(int id) { TestEntities db = new TestEntities(); UserInfo userInfo = db.UserInfo.Where<UserInfo>(u => u.ID == id).SingleOrDefault(); if (userInfo != null) { return View(userInfo); } else { return Content("参数错误"); } } public ActionResult EditUserInfo(int id) { TestEntities db = new TestEntities(); UserInfo userInfo = db.UserInfo.Where<UserInfo>(u => u.ID == id).FirstOrDefault(); if (userInfo != null) return View(userInfo); else return Content("参数错误"); } [HttpPost] public ActionResult EditUserInfo(UserInfo userInfo) { TestEntities db = new TestEntities(); db.Entry<UserInfo>(userInfo).State = System.Data.EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } } }
添加视图
@model MvcApplicationStudy.Models.UserInfo @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>EditUserInfo</title> </head> <body> @using (Html.BeginForm()) { @Html.HiddenFor(model =>model.ID) @Html.LabelFor(model => model.UserName) <br /> @Html.EditorFor(model => model.UserName) <br /> @Html.LabelFor(model => model.UserPwd) <br /> @Html.EditorFor(model => model.UserPwd) <br /> @Html.LabelFor(model => model.RegTime) <br /> @Html.EditorFor(model => model.RegTime) <br /> <p> <input type="submit" value="Save" /> </p> } <div> @Html.ActionLink("Back to List", "Index") </div> </body> </html>
点击修改,运行结果
时间: 2024-10-30 10:19:02