鍍金池/ 教程/ C#/ ASP.Net MVC手腳架
ASP.Net MVC簡介
ASP.Net MVC過濾器
ASP.Net MVC視圖
ASP.Net MVC安全
ASP.Net MVC手腳架
ASP.Net MVC控制器
ASP.Net MVC與SQL Server數(shù)據(jù)庫操作
ASP.Net MVC NuGet包管理
ASP.Net MVC入門程序
ASP.Net MVC Razor
ASP.Net MVC Bootstrap
ASP.Net MVC單元測試
ASP.Net MVC動作
ASP.Net MVC模式
ASP.Net MVC選擇器
ASP.Net MVC開發(fā)環(huán)境配置
ASP.Net MVC生命周期
ASP.Net MVC模型綁定
ASP.Net MVC自托管(本地主機(jī)部署)
ASP.Net MVC驗(yàn)證
ASP.Net MVC緩存
ASP.Net MVC數(shù)據(jù)模型
ASP.Net MVC路由
ASP.Net MVC教程
ASP.Net MVC助手
ASP.Net MVC數(shù)據(jù)注解
ASP.Net MVC Web API

ASP.Net MVC手腳架

ASP.NET腳手架是ASP.NET Web應(yīng)用程序的代碼生成框架。 Visual Studio 2013包含用于MVC和Web API項(xiàng)目的預(yù)安裝代碼生成器。當(dāng)想快速添加與數(shù)據(jù)模型交互的代碼時,可以將腳手架添加到項(xiàng)目中。使用腳手架可以減少在項(xiàng)目中開發(fā)標(biāo)準(zhǔn)數(shù)據(jù)操作的時間。

正如在前面章節(jié)教程中所看到的,我們已經(jīng)為Index,Create,Edit操作創(chuàng)建了視圖,并且還需要更新操作方法。但ASP.Net MVC提供了一種使用腳手架創(chuàng)建所有這些視圖和操作方法的更簡單的方法。

我們來看一個簡單的例子。它創(chuàng)建一個包含模型類 - Employee的相同示例,但是這次將使用腳手架。

第1步 - 打開Visual Studio,然后單擊:文件 -> 新建 -> 項(xiàng)目 菜單選項(xiàng)。一個新的項(xiàng)目對話框打開。
第2步 - 在左側(cè)窗格中,選擇:模板 -> Visual C# -> Web
第3步 - 在中間窗格中,選擇“ASP.NET Web應(yīng)用程序”。
第4步 - 在名稱字段中輸入項(xiàng)目名稱:MVCScaffolding ,然后單擊確定 繼續(xù)。

將看到下面的對話框,要求設(shè)置ASP.NET項(xiàng)目的初始內(nèi)容。

第5步 - 為了簡單起見,選擇: 選項(xiàng),并在為以下項(xiàng)添加文件夾和核心引用’ 中選中MVC 復(fù)選框,然后單擊確定

它將使用最少的預(yù)定義內(nèi)容創(chuàng)建一個基本的MVC項(xiàng)目。

項(xiàng)目由Visual Studio創(chuàng)建后,將在“解決方案資源管理器” 窗口中看到許多文件和文件夾。

添加 Entity Framework 支持

第一步是安裝實(shí)體框架(Entity Framework)。右鍵單擊該項(xiàng)目,然后選擇:管理NuGet程序包 ,打開NuGet軟件包管理器 。它將打開“NuGet包管理器”,在搜索框中搜索Entity Framework。如下所示 -

選擇實(shí)體框架(Entity Framework),然后點(diǎn)擊“安裝(Install)” 按鈕。 它將打開預(yù)覽對話框。

點(diǎn)擊確定 繼續(xù)下一步 -

點(diǎn)擊“我接受”按鈕開始安裝,完成后如下 -

當(dāng)實(shí)體框架安裝成功后,可看到如上面的截圖所示的出窗口的消息。

添加模型

要添加模型,請右鍵單擊解決方案資源管理器 中的Models 文件夾,然后選擇:添加 -> ,會看到添加新項(xiàng)目 對話框。

在中間平移中選擇:,并在名稱字段中輸入:Employee.cs。如下圖所示 -

使用以下代碼將一些屬性添加到Employee類。

using System;

namespace MVCScaffolding.Models {
   public class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime JoiningDate { get; set; }
      public int Age { get; set; }
   }
}

添加DBContext

經(jīng)過上一步,我們已經(jīng)創(chuàng)建了一個Employee模型,現(xiàn)在需要在這個模型類上添加另一個類,它將與實(shí)體框架(Entity Framework)進(jìn)行通信來檢索和保存數(shù)據(jù)。 以下是Employee.cs文件中的完整代碼。

using System;
using System.Data.Entity;

namespace MVCScaffolding.Models{
   public class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime JoiningDate { get; set; }
      public int Age { get; set; }
   }

   public class EmpDBContext : DbContext{
      public DbSet<Employee> Employees { get; set; }
   }
}

如上面所看到的EmpDBContext是從一個名為DbContextEF類派生的。 在這個類中,有一個名為DbSet的屬性,它基本上代表了要查詢和保存的實(shí)體。

添加腳手架項(xiàng)目

要添加腳手架,請右鍵單擊解決方案管理器中的Controllers文件夾,然后選擇:添加 -> 新建搭建基架項(xiàng)目 ,如下圖所示 -

它將顯示“添加腳手架”對話框 -

選擇:包含視圖MVC 5控制器(使用Entity Framework) 項(xiàng),然后單擊“添加” 按鈕,這將顯示添加控制器對話框。

模型類 下拉列表中選擇:Employee,并從數(shù)據(jù)上下文類 下拉列表中選擇EmpDBContext。還將看到默認(rèn)情況下選擇了控制器名稱。

單擊“添加”按鈕繼續(xù),在由Visual Studio使用腳手架創(chuàng)建的EmployeesController中看到以下代碼。

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MVCScaffolding.Models;

namespace MVCScaffolding.Controllers
{
    public class EmployeesController : Controller
    {
        private EmpDBContext db = new EmpDBContext();

        // GET: Employees
        public ActionResult Index()
        {
            return View(db.Employees.ToList());
        }

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

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

        // POST: Employees/Create
        // 為了防止“過多發(fā)布”攻擊,請啟用要綁定到的特定屬性,有關(guān) 
        // 詳細(xì)信息,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,Name,JoiningDate,Age")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(employee);
        }

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

        // POST: Employees/Edit/5
        // 為了防止“過多發(fā)布”攻擊,請啟用要綁定到的特定屬性,有關(guān) 
        // 詳細(xì)信息,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,JoiningDate,Age")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(employee);
        }

        // GET: Employees/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            return View(employee);
        }

        // POST: Employees/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Employee employee = db.Employees.Find(id);
            db.Employees.Remove(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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

運(yùn)行應(yīng)用程序并使用瀏覽器打開URL: http://localhost:59083/Employees ,將看到以下輸出結(jié)果 -

可以看到視圖中并沒有數(shù)據(jù),因?yàn)槲覀冞€沒有向 Visual Studio 創(chuàng)建的數(shù)據(jù)庫添加任何記錄。

下面點(diǎn)擊添加(Create New)鏈接來一個記錄,它將顯示創(chuàng)建視圖,填入相關(guān)信息 -

點(diǎn)擊“創(chuàng)建”按鈕,它將更新Index 視圖,看到有一條記錄。如下所示-

也可以看到新記錄也被添加到數(shù)據(jù)庫中。

通過上面所看實(shí)現(xiàn)步驟可以看到,我們已經(jīng)通過使用腳手架實(shí)現(xiàn)了一個簡單的例子,這是一個用來創(chuàng)建模型類,視圖和動作方法的更容易方式。


上一篇:ASP.Net MVC動作下一篇:ASP.Net MVC Web API