细节 - 控制器部分

通过 url ~/Student/Details/5 :(〜:site root,Student:Controller,Details:Action,5:student id),可以通过其 id 检索学生。

// GET: Student/Details/5
    public ActionResult Details(int? id)
    {
        // it good practice to consider that things could go wrong so,it is wise to have a validation in the controller
        if (id == null)
        {
            // return a bad request
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Student student = db.Students.Find(id);
        if (student == null)
        {
            // if doesn't found return 404
            return HttpNotFound();
        }
        return View(student);
    }