using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web; using System.Web.Mvc; using WebApplication1.Models; namespace WebApplication1.Controllers { public class LeaveMsgController:Controller { //GET:/LeaveMsg/ public ActionResult Index() { //Controllers向視圖傳值 //原理:先存數據然後再在視圖你面取出來 string s = "hello!!"; ViewData["aa"] = s; BBSDataContext bbs = new BBSDataContext(); List<student> v = (from m in bbs.student select m).ToList(); ViewData["list"] = v; return View(); } //刪除 public ActionResult Delete() { //RouterData 可以獲取路由上的數據 string id = RouteData.Values["id"].ToString(); //可以將一段文字返回給瀏覽器 //return view() 是把當前這個Action對應的視圖返回給瀏覽器 BBSDataContext dc = new BBSDataContext(); var v = from d in dc.student where d.stuID == int.Parse(id) select d; if (v.Count()>0) { //刪除 try { dc.student.DeleteOnSubmit(v.First()); dc.SubmitChanges(); //成功 //TempData臨時數據,為下一次請求服務,在下一次請求完畢,數據清除 //本質是session存放數據 TempData["msg"] = "刪除成功"; } catch (Exception) { //失敗 TempData["msg"] = "刪除失敗!!!"; } } else { //沒有對應的數據 TempData["msg"] = "沒有找到對應的數據!!!"; } return RedirectToAction("Index"); } //新增 public ActionResult Insert() { return View(); } //數據驗證 public void CheckData(student s) { //用戶名不能為空 if (s.stuName=="") { ModelState.AddModelError("stuName", "用戶名不能為空"); } //性別內容長度不能為空且不能大於2個字元 if (s.stuNo==""&&s.stuNo.Length<2) { ModelState.AddModelError("stuName", "用戶名不能為空或字元數大於2"); } //正則表達式 //bool b= Regex.IsMatch(s.stuID.ToString(), "^\\{6}$");//C#正則 } //提交新增操作 public ActionResult InsertOK() { //數據驗證 BBSDataContext dc = new BBSDataContext(); student s = new student(); s.stuName = Request["stuName"]; s.stuID = int.Parse(Request["stuNo"]); s.sex = Request["sex"]; s.remark = Request["remark"]; CheckData(s); if (!ModelState.IsValid)//驗證不通過 { ViewData["stuName"] = s.stuName; ViewData["stuNo"] = s.stuNo; ViewData["sex"] = s.sex; return View("Insert"); } try { dc.student.InsertOnSubmit(s); dc.SubmitChanges(); TempData["msg"] = "新增成功!!!"; return RedirectToAction("Index"); } catch (Exception) { TempData["msg"] = "新增失敗"; return RedirectToAction("Insert"); } } //編輯跳轉 public ActionResult Edit() { string id = RouteData.Values["id"].ToString(); ViewData["id"] = id; //找id對應的數據 BBSDataContext dc = new BBSDataContext(); var v = from d in dc.student where d.stuID == int.Parse(id) select d; if (v.Count()>0) { var d = v.First(); ViewData["stuName"] = d.stuName; ViewData["stuNo"] = d.stuNo; ViewData["sex"] = d.sex; ViewData["remark"] = d.remark; return View(); } else { TempData["msg"] = "沒有對應的數據"; return View(); } } //修改 public ActionResult Update() { string id = Request["id"]; BBSDataContext dc = new BBSDataContext(); var v = from d in dc.student where d.stuID == int.Parse(id) select d; if (v.Count()>0) { var m = v.First(); m.stuName = Request["stuName"]; m.stuNo = Request["stuNo"]; m.sex = Request["sex"]; CheckData(m); if (!ModelState.IsValid) { return View("Edit"); } try { dc.SubmitChanges();//提交修改 TempData["msg"] = "修改成功"; return RedirectToAction("Index"); } catch (Exception) { TempData["msg"] = "修改失敗!"; return RedirectToAction("Edit",new { id=id}); } } else { //沒有此數據 TempData["msg"] = "提交數據之前數據被其他用戶已刪除!!"; return RedirectToAction("Index"); } } } }控制器中業務邏輯
@{ ViewBag.Title = "Index"; } @using WebApplication1.Models <h2>學生列表</h2> @ViewData["aa"] @TempData["msg"] <table width="100%"> <tr><th>姓名</th><th>學號</th><th>性別</th><th>操作</th></tr> @{ List<student> list = ViewData["list"] as List<student>; } @{ foreach (var item in list) { <tr> <td>@item.stuName</td> <td>@item.stuID</td> <td>@item.sex</td> <td>@* 在MVC里提供了2個helper類,htmlHelper,UrlHelper類可以生成html代碼和url代碼,使用比較方便 <a href='student/delete'/@item.stuID>刪除</a> @Url.Action("delete", "student", new {id=item.stuID})*@ @Html.ActionLink("編輯", "Edit", new {id=item.stuID})| @Html.ActionLink("刪除", "Delete", new { id = item.stuID }) </td> </tr> } } </table> @Html.ActionLink("新增","Insert")Index頁面
@{ ViewBag.Title = "Insert"; } <h2>新增頁面</h2> @TempData["msg"] <p> @Html.ValidationSummary(); </p> @{ using (Html.BeginForm("InsertOK","LeaveMsg", FormMethod.Post)) {//創建的對象會在using語句塊結束時關閉對象 <span>用戶名</span> @Html.TextBox("stuName")<br /> <span>學號</span> @Html.TextBox("stuNo")<br /> <span>性別</span> @Html.TextBox("sex")<br /> <span>評論</span> @Html.TextArea("remark",null,5,30,null)<br /> <input type="submit" value="新增" /> @Html.ActionLink("返回列表", "Index"); } }新增頁面
@{ ViewBag.Title = "Edit"; } <h2>編輯信息</h2> @TempData["msg"] <p> @Html.ValidationSummary(); </p> @{ using (Html.BeginForm("Update", "LeaveMsg", FormMethod.Post)) { @Html.Hidden("id") <span>用戶名</span> @Html.TextBox("stuName")<br /> <span>學號</span> @Html.TextBox("stuNo")<br /> <span>性別</span> @Html.TextBox("sex")<br /> <span>評論</span> @Html.TextArea("remark", null, 5, 30, null)<br /> <input type="submit" value="修改" /> @Html.ActionLink("返回列表", "Index"); } }編輯頁面