文章出處

  在項目解決方案中,添加一個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 


文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()