强类型对一张进行增删查改
Posted Sealee
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了强类型对一张进行增删查改相关的知识,希望对你有一定的参考价值。
控制器
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using MVCDemo.Models; namespace MVCDemo.Controllers { public class StrongController : Controller { NorthwindDBDataContext dc = new NorthwindDBDataContext(); // // GET: /Strong/ public ActionResult Index() { ViewData.Model = dc.Users.AsEnumerable(); //显示数据页 return View(); } [HttpGet] //这里有重载,可能会不知道用那个方法 get方法 public ActionResult Create() { return View(); //跳到添加页面做添加 } [HttpPost] public ActionResult Create(Users user) { dc.Users.InsertOnSubmit(user); dc.SubmitChanges(); return RedirectToAction("Index"); //添加成功,跳到显示页 } public ActionResult Delete(int? id) { dc.Users.DeleteOnSubmit(dc.Users.Where(x => x.ID == id).First()); //找到你需要删除的用户 dc.SubmitChanges(); return RedirectToAction("Index"); } [HttpGet] public ActionResult Update(int? id) { ViewData.Model = dc.Users.Where(x => x.ID == id).First(); //找到需要更新的用户信息 return View(); //在更新页上显示数据 } [HttpPost] public ActionResult Update(Users user) { var result= dc.Users.Where(x => x.ID == user.ID); //找到需要更新的用户 foreach (var item in result) //进行更新 { item.UserName = user.UserName; item.UserPwd=user.UserPwd; item.Sex = user.Sex; } dc.SubmitChanges(); return RedirectToAction("Index"); } } }
显示数据页
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MVCDemo.Models.Users>>" %> <%-- 确定为用户集的类型 --%> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Index</title> <style> table { background-color: red; } table tr { background-color: white; } </style> </head> <body> <div> <table> <tr> <th>ID</th> <th>Name</th> <th>Pwd</th> <th>Sex</th> <th>更新</th> <th>删除</th> </tr> <% foreach (var item in Model) { %> <tr> <td><%= item.ID %></td> <td><%= item.UserName %></td> <td><%= item.UserPwd %></td> <td><%= item.Sex %></td> <td><%= Html.ActionLink("更新", "Update", new { id = item.ID })%></td> <td><%= Html.ActionLink("删除", "Delete", new { id = item.ID })%></td> </tr> <% } %> </table> </div> <%= Html.ActionLink("添加","Create") %> </body> </html>
添加
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCDemo.Models.Users>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Create</title> </head> <body> <script src="<%: Url.Content("~/Scripts/jquery-1.8.2.min.js") %>"></script> <script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>"></script> <script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"></script> <% using (Html.BeginForm("Create","Strong","FormMethod=post")) { %> <%--确定表单的传值方式和提交后要去的方法--%> <%: Html.AntiForgeryToken() %> <%: Html.ValidationSummary(true) %> <fieldset> <legend>Users</legend> <div class="editor-label"> <%: Html.LabelFor(model => model.UserName) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.UserName) %> //EditorFor可以根据数据库的类型来限制输入的类型 <%: Html.ValidationMessageFor(model => model.UserName) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.UserPwd) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.UserPwd) %> <%: Html.ValidationMessageFor(model => model.UserPwd) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Sex) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.Sex) %> 男 <%: Html.ValidationMessageFor(model => model.Sex) %> </div> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%: Html.ActionLink("Back to List", "Index") %> </div> </body> </html>
更新
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MVCDemo.Models.Users>" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Update</title> </head> <body> <script src="<%: Url.Content("~/Scripts/jquery-1.8.2.min.js") %>"></script> <script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>"></script> <script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"></script> <% using (Html.BeginForm("Update", "Strong", "FormMethod=post")) { %> <fieldset> <legend>Users</legend> <%: Html.HiddenFor(model => model.ID) %> <div class="editor-label"> <%: Html.LabelFor(model => model.UserName) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.UserName) %> <%: Html.ValidationMessageFor(model => model.UserName) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.UserPwd) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.UserPwd) %> <%: Html.ValidationMessageFor(model => model.UserPwd) %> </div> <div class="editor-label"> <%: Html.LabelFor(model => model.Sex) %> </div> <div class="editor-field"> <%: Html.EditorFor(model => model.Sex) %> <%: Html.ValidationMessageFor(model => model.Sex) %> </div> <p> <input type="submit" value="Save" /> </p> </fieldset> <% } %> <div> <%: Html.ActionLink("Back to List", "Index") %> </div> </body> </html>
以上是关于强类型对一张进行增删查改的主要内容,如果未能解决你的问题,请参考以下文章