zoukankan      html  css  js  c++  java
  • 初试 MVC+Boostrap

    MVC模式下Models文件下Student 学生表的相关类

    MVC模式控制器下的StudentController 中的代码

    // GET: Student  
    public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)

    {
      ViewBag.CurrentSort = sortOrder;
      ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
      ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

      if (searchString != null)
      {
        page = 1;
      }
      else
      {
        searchString = currentFilter;
         }

           ViewBag.CurrentFilter = searchString;

      var students = from s in db.Students
      select s;
      if (!String.IsNullOrEmpty(searchString))
      {
      students = students.Where(s => s.LastName.Contains(searchString)
      || s.FirstMidName.Contains(searchString));
      }
      switch (sortOrder)
      {
        case "name_desc":
        students = students.OrderByDescending(s => s.LastName);
        break;
        case "Date":
        students = students.OrderBy(s => s.EnrollmentDate);
        break;
        case "date_desc":
        students = students.OrderByDescending(s => s.EnrollmentDate);
        break;
        default: // Name ascending
        students = students.OrderBy(s => s.LastName);
        break;
      }

      int pageSize = 3;
      int pageNumber = (page ?? 1);
      return View(students.ToPagedList(pageNumber, pageSize));
    }


    // GET: Student/Details/5
    public ActionResult Details(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Student student = db.Students.Find(id);
      if (student == null)
      {
      return HttpNotFound();
      }
      return View(student);
    }

    // GET: Student/Create
    public ActionResult Create()
    {
      return View();
    }

    // POST: Student/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "LastName, FirstMidName, EnrollmentDate")]Student student)
    {
      try
      {
        if (ModelState.IsValid)
        {
        db.Students.Add(student);
        db.SaveChanges();
        return RedirectToAction("Index");
        }
      }
      catch (RetryLimitExceededException /* dex */)
      {
      //Log the error (uncomment dex variable name and add a line here to write a log.
      ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
      }
      return View(student);
    }

    // GET: Student/Edit/5
    public ActionResult Edit(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      Student student = db.Students.Find(id);
      if (student == null)
      {
        return HttpNotFound();
      }
      return View(student);
    }

    // POST: Student/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost, ActionName("Edit")]
    [ValidateAntiForgeryToken]
    public ActionResult EditPost(int? id)
    {
      if (id == null)
      {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      var studentToUpdate = db.Students.Find(id);
      if (TryUpdateModel(studentToUpdate, "",new string[] { "LastName", "FirstMidName", "EnrollmentDate" }))
      {
      try
      {
        db.Entry(studentToUpdate).State = EntityState.Modified;
        db.SaveChanges();

        return RedirectToAction("Index");
      }
      catch (RetryLimitExceededException /* dex */)
        {
          //Log the error (uncomment dex variable name and add a line here to write a log.
          ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
        }
      }
      return View(studentToUpdate);
    }

    // GET: Student/Delete/5
    public ActionResult Delete(int? id, bool? saveChangesError = false)
    {
      if (id == null)
      {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
      if (saveChangesError.GetValueOrDefault())
      {
        ViewBag.ErrorMessage = "Delete failed. Try again, and if the problem persists see your system administrator.";
      }
      Student student = db.Students.Find(id);
      if (student == null)
      {
        return HttpNotFound();
      }
      return View(student);
    }

    // POST: Student/Delete/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Delete(int id)
    {
      try
      {
        Student student = db.Students.Find(id);
        db.Students.Remove(student);
        db.SaveChanges();
      }
      catch (RetryLimitExceededException/* dex */)
      {
        //Log the error (uncomment dex variable name and add a line here to write a log.
        return RedirectToAction("Delete", new { id = id, saveChangesError = true });
      }
      return RedirectToAction("Index");
    }


    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
      db.Dispose();
      }
      base.Dispose(disposing);
    }

    MVC模式下的Views视图代码

    Index.cshtml 

    Add.cshtml

    Delect.cshtml

     

    Edit.cshtml

    其上代码实现如下图,功能全部实现。

  • 相关阅读:
    DOS下读取4GB内存
    开始了
    PHP函数补完:var_export()
    php 调试相关
    css选择符
    javascript的urlencode
    用CSS代码绘制三角形 纯CSS绘制三角形的代码
    Jquery 操作Cookie
    提高PHP编程效率的53种方法
    执行phpinfo();时提示:date_default_timezone_set()
  • 原文地址:https://www.cnblogs.com/liuruipeng/p/7598899.html
Copyright © 2011-2022 走看看