2016-06-22
這篇是參考下面這篇的學習,不過工具從 Visual Studio 2013 改成 Visual Studio 2015 with Update 2 繁體中文版;網頁上有提到一篇新的改用 Visual Studio 2015,但是那篇要另外安裝 ASP.NET Core,而目前最新為 ASP.NET Code 1.0.0 RC2,並非正式版,所以暫時不想安裝和參考那篇。
內容不是完全翻譯,因為練習的心得,有增加、刪減圖片和文字。
Getting Started with ASP.NET MVC 5
http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
請先看這篇
[研究] ASP.NET MVC 5入門(八)增加搜尋 Method 和 View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
********************************************************************************
1. Setting up Code First Migrations for Model Changes
(下圖) 輸入下面指令
Enable-Migrations -ContextTypeName MvcMovie.Models.MovieDBContext |
(下圖) Enable-Migrations 指令會建立 Configuration.cs 檔案
修改 Migrations\Configuration.cs 檔案,把 Seed() 從
protected override void Seed(MvcMovie.Models.MovieDBContext context) { // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // } |
protected override void Seed(MvcMovie.Models.MovieDBContext context) { context.Movies.AddOrUpdate( i => i.Title, new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-1-11"), Genre = "Romantic Comedy", Price = 7.99M }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M }, new Movie { Title = "Ghostbusters 2", ReleaseDate = DateTime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M } ); } |
按下 Ctrl-Shift-B 重建方案 (必須),在 套件管理器主控台 (Package Manager Console) 輸入
add-migration Initial |
add-migration Initial 會在 Migrations folder 資料夾建立 {DateStamp}_Initial.cs 檔案
在 套件管理器主控台 (Package Manager Console) 輸入
update-database |
********************************************************************************
2. Adding a Rating Property to the Movie Model
在 Models\Movie.cs 增加 public string Rating { get; set; }
public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } public string Rating { get; set; } } |
找到 Controllers\MoviesController.cs 的
[Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] |
[Bind(Include = "ID,Title,ReleaseDate,Genre,Price,Rating")] |
修改 \Views\Movies\Index.cshtml 成下面
@model IEnumerable<MvcMovie.Models.Movie> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") @using (Html.BeginForm()) { <p> Genre: @Html.DropDownList("movieGenre", "All") Title: @Html.TextBox("SearchString") <br /> <input type="submit" value="Filter" /> </p> } </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.ReleaseDate) </th> <th> @Html.DisplayNameFor(model => model.Genre) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th> @Html.DisplayNameFor(model => model.Rating) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.ReleaseDate) </td> <td> @Html.DisplayFor(modelItem => item.Genre) </td> <td> @Html.DisplayFor(modelItem => item.Price) </td> <td> @Html.DisplayFor(modelItem => item.Rating) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table> |
修改 \Views\Movies\Create.cshtml 最後部分內容 (原始網頁標錯顏色了,下面才對)
執行會出錯
<div class="form-group"> @Html.LabelFor(model => model.Price, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Price, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Price, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Rating, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Rating) @Html.ValidationMessageFor(model => model.Rating) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Create" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") } |
執行會出錯
開啟 Migrations\Configuration.cs,每個 Moive 都要增加 Rating 欄位
new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-1-11"), Genre = "Romantic Comedy", Rating = "PG", Price = 7.99M }, |
在 套件管理器主控台 (Package Manager Console) 輸入
add-migration Rating |
在下面兩個檔案
Migrations\201606220820407_Initial.cs
Migrations\201606220858569_Rating.cs
的 public override void Up() 裡面會多程式碼
( 原始網頁內容和實際測試有些不同)
在 套件管理器主控台 (Package Manager Console) 輸入
update-database |
(下圖) 執行成功,多了 Rating 欄位
按下 Create New 測試,或 Edit 測試,應該都正常。
(完)
[研究] ASP.NET MVC 5入門(一)開始
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5_21.html
[研究] ASP.NET MVC 5入門(二)增加控制器(Controller)
[研究] ASP.NET MVC 5入門(三)增加檢視(View)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5view.html
[研究] ASP.NET MVC 5入門(六)從 Controller 存取 Model 的資料
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-controller-model.html
[研究] ASP.NET MVC 5入門(四)增加模型(Model)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5model.html
[研究] ASP.NET MVC 5入門(五)建立 SQL Server LocalDB 的連線字串
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-sql-server-localdb.html
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5model.html
[研究] ASP.NET MVC 5入門(五)建立 SQL Server LocalDB 的連線字串
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-sql-server-localdb.html
[研究] ASP.NET MVC 5入門(六)從 Controller 存取 Model 的資料
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-controller-model.html
[研究] ASP.NET MVC 5入門(七)瞭解 Edit Method 和 Edit View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-edit-method-edit-view.html
[研究] ASP.NET MVC 5入門(八)增加搜尋 Method 和 View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-edit-method-edit-view.html
[研究] ASP.NET MVC 5入門(八)增加搜尋 Method 和 View
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5-method-view.html
[研究] ASP.NET MVC 5入門(九)增加新欄位
沒有留言:
張貼留言