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入門(一)開始
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5_21.html
[研究] ASP.NET MVC 5入門(二)增加控制器(Controller)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5controller.html
[研究] ASP.NET MVC 5入門(三)增加檢視(View)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5view.html
[研究] ASP.NET MVC 5入門(四)增加模型(Model)
http://shaurong.blogspot.com/2016/06/aspnet-mvc-5model.html
http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller[研究] 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-5-sql-server-localdb.html
6.Accessing Your Model's Data from a Controller
********************************************************************************
MVC 對應
M (Model) : Models\Movies.cs
V (View) : Views\Shared 預設共用目錄
Views\Movies\Create.cshtml
Views\Movies\Delete.cshtml
Views\Movies\Details.cshtml
Views\Movies\Edit.cshtml
Views\Movies\Index.cshtml
C (Controller) : Controllers\MoviesController.cs
Controllers\MoviesController.cs 產生內容如下
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 MvcMovie.Models; namespace MvcMovie.Controllers { public class MoviesController : Controller { private MovieDBContext db = new MovieDBContext(); // GET: Movies public ActionResult Index() { return View(db.Movies.ToList()); } // 下面會對應 Views\Movies\Details.cshtml // GET: Movies/Details/5 public ActionResult Details(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } // GET: Movies/Create public ActionResult Create() { return View(); } // POST: Movies/Create // 若要免於過量張貼攻擊,請啟用想要繫結的特定屬性,如需 // 詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) { if (ModelState.IsValid) { db.Movies.Add(movie); db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } // GET: Movies/Edit/5 public ActionResult Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } // POST: Movies/Edit/5 // 若要免於過量張貼攻擊,請啟用想要繫結的特定屬性,如需 // 詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=317598。 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include = "ID,Title,ReleaseDate,Genre,Price")] Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } // GET: Movies/Delete/5 public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } // POST: Movies/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Movie movie = db.Movies.Find(id); db.Movies.Remove(movie); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } } } |
Views\Movies\Details.cshtml 內容如下
@model MvcMovie.Models.Movie @{ ViewBag.Title = "Details"; } <h2>Details</h2> <div> <h4>Movie</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Title) </dt> <dd> @Html.DisplayFor(model => model.Title) </dd> <dt> @Html.DisplayNameFor(model => model.ReleaseDate) </dt> <dd> @Html.DisplayFor(model => model.ReleaseDate) </dd> <dt> @Html.DisplayNameFor(model => model.Genre) </dt> <dd> @Html.DisplayFor(model => model.Genre) </dd> <dt> @Html.DisplayNameFor(model => model.Price) </dt> <dd> @Html.DisplayFor(model => model.Price) </dd> </dl> </div> <p> @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) | @Html.ActionLink("Back to List", "Index") </p> |
Views\Movies\Indexs.cshtml 內容如下
@model IEnumerable<MvcMovie.Models.Movie>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</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></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.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>
|
使用 SQL Server 來工作
(完)
[研究] 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
作者已經移除這則留言。
回覆刪除