基本都要使用C控制器中的兩個action來完成操作,一個用於從主界面跳轉到新頁面、同時將所需操作的數據傳到新界面,另一個則對應新界面的按鈕,用於完成操作、將數據傳回主界面以及跳轉回主界面。根據不同情況使用不同的傳值方法。
在M模型層中定義所需的LinQ操作
1.查
1 <table style="width:100%;background-color:#0ff;text-align:center"> 2 <tr> 3 <td>賬號</td> 4 <td>密碼</td> 5 <td>昵稱</td> 6 <td>性別</td> 7 <td>生日</td> 8 <td>民族</td> 9 <td>操作</td> 10 </tr> 11 <% 12 List<User> list = new UserData().Select(); 13 foreach(User U in list){ 14 %> 15 <tr style="background-color:#cefef8"> 16 <td><%=U.UserName %></td> 17 <td><%=U.PassWord %></td> 18 <td><%=U.NickName%></td> 19 <td><%=U.Sexstr %></td> 20 <td><%=U.birStr %></td> 21 <td><%=U.Nation1.NationName %></td> 22 <td> <a href="Home/Update/<%=U.UserName %>">修改</a> 23 <a class="del" href="Home/Delete?haha=<%=U.UserName %>">刪除</a></td> 24 </tr> 25 <% }%> 26 </table>
2.刪
在C層添加動作
1 public void Delete(string uname) 2 { 3 User u = con.User.Where(r => r.UserName == uname).FirstOrDefault(); 4 if (u != null) 5 { 6 con.User.DeleteOnSubmit(u); 7 con.SubmitChanges(); 8 } 9 }
3.添加
View中提交元素,表單元素使用form表單提交,按鈕的使用submit,點擊submit的時候會提交所在form表單中的數據,在控制器C中獲取元素,在模型層M的寫法,在C中調用。

<form name="form1" action="Insert1" method="post"> <%string a = ""; %> <h1>添加用戶</h1> 用戶名:<input type="text" name="username" /><br /> 密碼:<input type="text" name="password" /><br /> 昵稱:<input type="text" name="nickname" /><br /> 性別:<input type="text" name="sex" /><br /> 生日:<input type="text" name="birthday" /><br /> 民族:<input type="text" name="nation" /><br /> <input type="submit" value="添加" /> </form>
1 public ActionResult Insert() 2 { 3 return View(); 4 } 5 6 public ActionResult Insert1(string username, string password, string nickname, string sex, string birthday, string nation) 7 { 8 Users u = new Users(); 9 u.UserName = username; 10 u.PassWord = password; 11 u.NickName = nickname; 12 u.Sex = Convert.ToBoolean(sex); 13 u.Birthday = Convert.ToDateTime(birthday); 14 u.Nation = nation; 15 16 new UsersData().Insert(u); 17 18 return RedirectToAction("Index", "Home"); 19 }//接收form表單提交的數據
4.修改
同添加,需要兩個action支持,一個主頁面打開修改頁面,一個修改按鈕確定修改返回主頁面
從控制器傳值到View使用ViewBag.包名=數據源。
View中<%Users u=ViewBag.包名 as User; %>
系統自生成的Users u 有可能缺少部分內容
form表單中的action路徑 action="/home/update"
1 public ActionResult Update(string id) 2 { 3 User u = new UserData().Select(id); 4 5 ViewBag.heihei1 = u; 6 return View(); 7 } 8 public ActionResult Update1(User u) 9 { 10 new UserData().Update(u); 11 12 return RedirectToAction("Index"); 13 }