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

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

  • 相关阅读:
    485串口接线
    mvc3 升级mvc5
    VB连接ACCESS数据库,使用 LIKE 通配符问题
    VB6 读写西门子PLC
    可用的 .net core 支持 RSA 私钥加密工具类
    解决 Win7 远程桌面 已停止工作的问题
    解决 WinForm 重写 CreateParams 隐藏窗口以后的显示问题
    解决安装 .net framework 发生 extracting files error 问题
    CentOS7 安装配置笔记
    通过特殊处理 Resize 事件解决 WinForm 加载时闪烁问题的一个方法
  • 原文地址:https://www.cnblogs.com/liuruipeng/p/7598899.html
Copyright © 2011-2022 走看看