ASP.NET MVC系列:从Controller访问Model数据

  在项目解决方案中,添加一个MoviesController控制器,选择对应的模板,和模型类以及数据上下文;关于如何添加模型类和数据上下文,我们在ASP.NET MVC系列:添加模型中已经介绍过

  点击确定之后,你会发现Visual Studio已经为你创建好了对数据的增、删、改、查方法,并且生成了对应的视图

  运行程序后,你可以直接打开http://localhost:60534/Movies主页,查看visual studio生成的功能

  在生成的控制器代码中你会发现,控制器中会创建MovieDBContext(数据库上下文)的一个实例,然后所有对数据的操作都是调用MovieDBContext实例的方法

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data;
  4 using System.Data.Entity;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Mvc;
  8 using MvcMovie.Models;
  9 
 10 namespace MvcMovie.Controllers
 11 {
 12     public class MoviesController : Controller
 13     {
 14         private MovieDBContext db = new MovieDBContext();
 15 
 16         //
 17         // GET: /Movies/
 18 
 19         public ActionResult Index()
 20         {
 21             return View(db.Movies.ToList());
 22         }
 23 
 24         //
 25         // GET: /Movies/Details/5
 26 
 27         public ActionResult Details(int id = 0)
 28         {
 29             Movie movie = db.Movies.Find(id);
 30             if (movie == null)
 31             {
 32                 return HttpNotFound();
 33             }
 34             return View(movie);
 35         }
 36 
 37         //
 38         // GET: /Movies/Create
 39 
 40         public ActionResult Create()
 41         {
 42             return View();
 43         }
 44 
 45         //
 46         // POST: /Movies/Create
 47 
 48         [HttpPost]
 49         [ValidateAntiForgeryToken]
 50         public ActionResult Create(Movie movie)
 51         {
 52             if (ModelState.IsValid)
 53             {
 54                 db.Movies.Add(movie);
 55                 db.SaveChanges();
 56                 return RedirectToAction("Index");
 57             }
 58 
 59             return View(movie);
 60         }
 61 
 62         //
 63         // GET: /Movies/Edit/5
 64 
 65         public ActionResult Edit(int id = 0)
 66         {
 67             Movie movie = db.Movies.Find(id);
 68             if (movie == null)
 69             {
 70                 return HttpNotFound();
 71             }
 72             return View(movie);
 73         }
 74 
 75         //
 76         // POST: /Movies/Edit/5
 77 
 78         [HttpPost]
 79         [ValidateAntiForgeryToken]
 80         public ActionResult Edit(Movie movie)
 81         {
 82             if (ModelState.IsValid)
 83             {
 84                 db.Entry(movie).State = EntityState.Modified;
 85                 db.SaveChanges();
 86                 return RedirectToAction("Index");
 87             }
 88             return View(movie);
 89         }
 90 
 91         //
 92         // GET: /Movies/Delete/5
 93 
 94         public ActionResult Delete(int id = 0)
 95         {
 96             Movie movie = db.Movies.Find(id);
 97             if (movie == null)
 98             {
 99                 return HttpNotFound();
100             }
101             return View(movie);
102         }
103 
104         //
105         // POST: /Movies/Delete/5
106 
107         [HttpPost, ActionName("Delete")]
108         [ValidateAntiForgeryToken]
109         public ActionResult DeleteConfirmed(int id)
110         {
111             Movie movie = db.Movies.Find(id);
112             db.Movies.Remove(movie);
113             db.SaveChanges();
114             return RedirectToAction("Index");
115         }
116 
117         protected override void Dispose(bool disposing)
118         {
119             db.Dispose();
120             base.Dispose(disposing);
121         }
122     }
123 }
MoviesController

  在视图的页面的顶部,你会发现这样一行代码;这行代码的作用就是指定视图的对象模型

@model MvcMovie.Models.Movie 

  并且允许你在视图中访问和设置类的对象实例的数据成员   

 

  最后我们发现,visual studio还帮我们生成了本地数据库文件,以及数据库的表结构

  http://www.asp.net/mvc/overview/older-versions/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller 

posted @ 2016-03-23 23:14  高效养猪倌  阅读(1465)  评论(0编辑  收藏  举报