2016-06-21
這篇是參考下面這篇的學習,不過工具從 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
3. Adding a View
http://www.asp.net/mvc/overview/getting-started/introduction/adding-a-view
********************************************************************************
1.替 Controller\HelloWorldController.cs 控制器的 Index() 增加 Index.cshtml 檢視
MVC 對應
M (Model) :
V (View) : Views\Shared 預設共用目錄
Views\HelloWorld 目錄
C (Controller) : Controllers\HelloWorldController.cs 內容
把 Controllers\HelloWorldController.cs 的 Index() 方法從
public string Index() { return "This is my <b>default</b> action..."; } |
改回
public ActionResult Index()
{
return View();
}
|
PS:
(1) Views\HelloWorld 目錄裡面沒有任何 View,預設抓取 Views\Shared\_Layout.cshtml 網頁內容
(2) 新增的 Views\HelloWorld\index.cshtml 內容如下:
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } |
(3) Your application description page 是 Controllers\HomeController.cs 的內容
(4) Use this area to provide additional information 是 Views\Home\about.cshtml 的內容
********************************************************************************
2.客製化 Index.cshtml View
MVC 對應
M (Model) :
V (View) : Views\Shared 預設共用目錄
Views\HelloWorld\index.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
修改 Views\HelloWorld\index.cshtml 內容
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } |
成為
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p>Hello from our View Template!</p> |
表示顯示完成 _Layout.cshtml 內容後,多顯示其它的內容,
ViewBag.Title 值會傳送給 _Layout.cshtml,
********************************************************************************
3. 修改 Views\Shared\_Layout.cshtml 共用檢視
MVC 對應
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
修改 Views\Shared\_Layout.cshtml 內容
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - 我的 ASP.NET 應用程式</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("應用程式名稱", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("首頁", "Index", "Home")</li> <li>@Html.ActionLink("關於", "About", "Home")</li> <li>@Html.ActionLink("連絡方式", "Contact", "Home")</li> </ul> @Html.Partial("_LoginPartial") </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - 我的 ASP.NET 應用程式</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html> |
成為
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title - Movie App</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> @Html.ActionLink("MVC Movie", "Index", "Movies", new { area = "" }, new { @class = "navbar-brand" }) </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li>@Html.ActionLink("首頁", "Index", "Home")</li> <li>@Html.ActionLink("關於", "About", "Home")</li> <li>@Html.ActionLink("連絡方式", "Contact", "Home")</li> </ul> @Html.Partial("_LoginPartial") </div> </div> </div> <div class="container body-content"> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - 我的 ASP.NET 應用程式</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html> |
(下圖) 不管哪個畫面,上方標題變成 MVC Movie 了
********************************************************************************
4. 再次修改 Views\HelloWorld\index.cshtml
MVC 對應
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
修改 Views\HelloWorld\index.cshtml 內容
@*@{ Layout = "~/Views/Shared/_Layout.cshtml"; }*@ @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p>Hello from our View Template!</p> |
成為
@{ Layout = "~/Views/Shared/_Layout.cshtml"; } @{ ViewBag.Title = "Movie List"; } <h2>My Movie List</h2> <p>Hello from our View Template!</p> |
********************************************************************************
5. 從 Controller 傳送資料到 View
替 HelloWorldController.cs 修改 Welcome(),並且新增 Views\HelloWorld\Welcome.cshtml,修改 Views 內容,然後看結果。
5. 從 Controller 傳送資料到 View
替 HelloWorldController.cs 修改 Welcome(),並且新增 Views\HelloWorld\Welcome.cshtml,修改 Views 內容,然後看結果。
MVC 對應
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\HelloWorld\Welcome.cshtml (新增)
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
原來Controllers\HelloWorldController.cs 內容
M (Model) :
V (View) : Views\HelloWorld\index.cshtml
Views\HelloWorld\Welcome.cshtml (新增)
Views\Shared\_Layout.cshtml
C (Controller) : Controllers\HelloWorldController.cs 內容
原來Controllers\HelloWorldController.cs 內容
public ActionResult Index()
{
return View();
}
using System.Web; using System.Web.Mvc; namespace MvcMovie.Controllers { public class HelloWorldController : Controller { // // GET: /HelloWorld/ public ActionResult Index() { return View(); } // // GET: /HelloWorld/Welcome/ public string Welcome(string name, int ID = 1) { return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID); } } } |
改為 (修改 Welcome() 方法)
public ActionResult Index()
{
return View();
}
using System.Web; using System.Web.Mvc; namespace MvcMovie.Controllers { public class HelloWorldController : Controller { public ActionResult Index() { return View(); } public ActionResult Welcome(string name, int numTimes = 1) { ViewBag.Message = "Hello " + name; ViewBag.NumTimes = numTimes; return View(); } } } |
目前 Welcome.cshtml 內容
@{
Layout = "~/Views/Shared/_Layout.cshtml";
} |
改為
[研究] 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)
沒有留言:
張貼留言