基本都要使用C控制器中的两个action来完成操作,一个用于从主界面跳转到新页面、同时将所需操作的数据传到新界面,另一个则对应新界面的按钮,用于完成操作、将数据传回主界面以及跳转回主界面。根据不同情况使用不同的传值方法。
在M模型层中定义所需的LinQ操作
1.查
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="Mvc_staff.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <table style="background-color:blue;width:100%;"> <tr style="color:white;text-align:center"> <td>用户名</td> <td>密码</td> <td>昵称</td> <td>性别</td> <td>生日</td> <td>民族</td> <td>操作</td> </tr> <% List<Users> ulist = new UsersData().SelectAll(); foreach (Users us in ulist) { %> <tr style="background-color:white;text-align:center;"> <td><%=us.Username %></td> <td><%=us.Password %></td> <td><%=us.Nikename %></td> <td><%=us.Sex.Value?"男":"女" %></td> <td><%=us.Birthday.Value.ToString("yyyy年MM月dd日") %></td> <td><%=us.Nation1.NationName %></td> <td> <a href="/Home/Update/<%=us.Ids %>">修改</a> <a href="Home/Delete/<%=us.Ids %>">删除</a> </td> </tr> <%} %> </table> <input type="button" value="添加" onclick="window.open('Home/InsertMain')" /> </body> </html>
2.删
在C层添加动作
public ActionResult Delete(string id) { bool ok = new UsersData().delete(id); Session["deleteok"]=ok; return RedirectToAction("Index"); }
3.添加
View中提交元素,表单元素使用form表单提交,按钮的使用submit,点击submit的时候会提交所在form表单中的数据,在控制器C中获取元素,在模型层M的写法,在C中调用。
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="Mvc_staff.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>InsertMain</title> <script src="jquery-1.7.1.min.js"></script> <script src="../../DatePicker/WdatePicker.js"></script> <style type="text/css"> #bottom { width: 300px; height: 500px; margin: 0 auto; border: 1px solid #808080; } #title { width: 100%; font-size: 20px; text-align: center; margin-top: 20px; } .itembot { width: 100%; height: 50px; border: 1px solid #808080; } .nav { width: 200px; height: 20px; float: right; } .item { height: 20px; font-size: 12px; line-height: 20px; float: right; } #sub { width:100%; margin-top:10px; font-size:14px; color:#808080; background-color:#0026ff; } </style> </head> <body> <form action="InsertSub" method="post"> <div id="bottom"> <div id="title">添加数据</div> <%-- 用户名 --%> <div class="itembot"> <div class="nav"> <input type="text" name="usersname" /> </div> <span class="item">用户名:</span> </div> <%-- 密码 --%> <div class="itembot"> <div class="nav"> <input type="password" name="password" /> </div> <span class="item">密码:</span> </div> <%-- 确认密码 --%> <div class="itembot"> <div class="nav"> <input type="password" /> </div> <span class="item">确认密码:</span> </div> <%-- 昵称 --%> <div class="itembot"> <div class="nav"> <input type="text" name="nickname" /> </div> <span class="item">昵称:</span> </div> <%-- 性别 --%> <div class="itembot"> <div class="nav"> <input type="radio" checked="checked" name="sex" value="true" id="ra_sex1" /> <label for="ra_sex1">男</label> <input type="radio" name="sex" value="false" id="ra_sex2" /> <label for="ra_sex2">女</label> </div> <span class="item">性别:</span> </div> <%-- 生日 --%> <div class="itembot"> <div class="nav"> <input type="text" name="birthday" id="txt_Time"/> </div> <span class="item">生日:</span> </div> <%-- 民族 --%> <div class="itembot"> <div class="nav"> <select name="nation"> <% List<Nation> nlist = new UsersData().NSelectAll(); foreach (Nation na in nlist) { %> <option value="<%=na.NationCode %>"><%=na.NationName %></option> <%} %> </select> </div> <span class="item">民族:</span> </div> <input type="submit" value="保存" id="sub"/> </div> </form> </body> </html> <script type="text/javascript"> $("#txt_Time").focus = function () { alert('1111'); WdatePicker({ readOnly: true, maxDate: '%y-%M-%d' }); } </script>
public ActionResult InsertMain() { return View(); } public ActionResult InsertSub(string usersname, string password, string nickname, string sex, string birthday, string nation) { Users us = new Users(); us.Username = usersname; us.Password = password; us.Nikename = nickname; us.Sex = Convert.ToBoolean(sex); us.Birthday = Convert.ToDateTime(birthday); us.Nation=nation; bool isok = new UsersData().insert(us); Session["Insertok"] = isok; return RedirectToAction("Index"); }
4.修改
同添加,需要两个action支持,一个主页面打开修改页面,一个修改按钮确定修改返回主页面
从控制器传值到View使用ViewBag.包名=数据源。
View中<%Users u=ViewBag.包名 as User; %>
系统自生成的Users u 有可能缺少部分内容
form表单中的action路径 action="/home/update"
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %> <%@ Import Namespace="Mvc_staff.Models" %> <!DOCTYPE html> <html> <head runat="server"> <meta name="viewport" content="width=device-width" /> <title>Update</title> <script src="jquery-1.7.1.min.js"></script> <style type="text/css"> #bottom { width: 300px; height: 500px; margin: 0 auto; border: 1px solid #808080; } #title { width: 100%; font-size: 20px; text-align: center; margin-top: 20px; } .itembot { width: 100%; height: 50px; border: 1px solid #808080; } .nav { width: 200px; height: 20px; float: right; } .item { height: 20px; font-size: 12px; line-height: 20px; float: right; } #sub { width:100%; margin-top:10px; font-size:14px; color:#808080; background-color:#0026ff; } </style> </head> <body> <% Users users = ViewBag.getusers ; %> <%if (users!=null) { %> <script type="text/javascript"> alert('123123'); </script> <%} %> <form action="/Home/UpdateSub" method="post"> <div id="bottom"> <div id="title">修改数据</div> <input type="hidden" name="ids" value="<%=users.Ids %>"/> <%-- 用户名 --%> <div class="itembot"> <div class="nav"> <input type="text" name="usersname" value="<%=users.Username %>"/> </div> <span class="item">用户名:</span> </div> <%-- 密码 --%> <div class="itembot"> <div class="nav"> <input type="password" name="password" value="<%=users.Password %>"/> </div> <span class="item">密码:</span> </div> <%-- 确认密码 --%> <div class="itembot"> <div class="nav"> <input type="password" value="<%=users.Password %>"/> </div> <span class="item">确认密码:</span> </div> <%-- 昵称 --%> <div class="itembot"> <div class="nav"> <input type="text" name="nickname" value="<%=users.Nikename %>"/> </div> <span class="item">昵称:</span> </div> <%-- 性别 --%> <div class="itembot"> <div class="nav"> <%if(users.Sex==true) { %> <input type="radio"checked="checked" name="sex" value="true" id="ra-sex1" /> <label for="ra-sex1">男</label> <input type="radio" name="sex" value="false" id="ra-sex2" /> <label for="ra-sex2">女</label> <%} else{ %> <input type="radio" name="sex" value="true" id="Radio1" /> <label for="ra-sex1">男</label> <input type="radio" checked="checked" name="sex" value="false" id="Radio2" /> <label for="ra-sex2">女</label> <%} %> </div> <span class="item">性别:</span> </div> <%-- 生日 --%> <div class="itembot"> <div class="nav"> <input type="text" name="birthday" id="txt_Time" value="<%=users.Birthday %>"/> </div> <span class="item">生日:</span> </div> <%-- 民族 --%> <div class="itembot"> <div class="nav"> <select name="nation"> <% List<Nation> nlist = new UsersData().NSelectAll(); foreach (Nation na in nlist) { if(na.NationCode==users.Nation) { %> <option selected="selected" value="<%=na.NationCode %>"><%=na.NationName %></option> <%} else{%> <option value="<%=na.NationCode %>"><%=na.NationName %></option> <%}} %> </select> </div> <span class="item">民族:</span> </div> <input type="submit" value="保存" id="sub"/> </div> </form> </body> </html>
public ActionResult Update(string id) { Users us = new UsersData().select(id); ViewBag.getusers = us; return View(); } public ActionResult UpdateSub(string usersname, string password, string nickname, string sex, string birthday, string nation,string ids) { bool isok = new UsersData().Update(usersname, password, nickname, sex, birthday, nation, ids); Session["updateok"] = isok; return RedirectToAction("Index","Home"); }
完整数据类
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Mvc_staff.Models { public class UsersData { /// <summary> /// 查询全部方法 /// </summary> /// <returns></returns> UsersDataContext con = new UsersDataContext(); public List<Users> SelectAll() { return con.Users.ToList(); } public List<Nation> NSelectAll() { return con.Nation.ToList(); } public bool insert(Users us) { bool ok = false; try { con.Users.InsertOnSubmit(us); con.SubmitChanges(); ok = true; } catch { }; return ok; } public bool delete(string ids) { bool ok = false; Users us=con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault(); if (us != null) { con.Users.DeleteOnSubmit(us); con.SubmitChanges(); ok = true; } return ok; } public Users select(string ids) { return con.Users.Where(r=>r.Ids.ToString()==ids).FirstOrDefault(); } public bool Update(string usersname, string password, string nickname, string sex, string birthday, string nation, string ids) { bool ok = false; try { Users ulist = con.Users.Where(r => r.Ids.ToString() == ids).FirstOrDefault(); if (ulist != null) { ulist.Ids =Convert.ToInt32(ids); ulist.Username = usersname; ulist.Password = password; ulist.Nikename = nickname; ulist.Sex =Convert.ToBoolean(sex) ; ulist.Birthday = Convert.ToDateTime(birthday); ulist.Nation = nation; con.SubmitChanges(); ok = true; } } catch { } return ok; } } }