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")
@{ 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"); } }