Asp.Net MVC4入門指南(3):添加一個視圖

來源:互聯網
上載者:User

在本節中,您需要修改HelloWorldController類,從而使用視圖模板檔案,乾淨優雅的封裝產生返回到用戶端瀏覽器HTML的過程。

您將建立一個視圖模板檔案,其中使用了ASP.NET MVC 3所引入的Razor視圖引擎。Razor視圖模板檔案使用.cshtml副檔名,並提供了一個優雅的方式來使用C#語言建立所要輸出的HTML。用Razor編寫一個視圖模板檔案時,將所需的字元和鍵盤敲擊數量降到了最低,並實現了快速,流暢的編碼工作流程。

當前在控制器類中的Index方法返回了一個硬式編碼字串。更改Index方法返回一個View對象,如下面的範例程式碼:

public ActionResult Index() {     return View(); }

上面的Index方法使用一個視圖模板來產生一個HTML返回給瀏覽器。控制器的方法(也被稱為action method(操作方法) ),如上面的Index方法,一般返回一個ActionResult(或從ActionResult所繼承的類型),而不是原始的類型,如字串。

在該項目中,您可以使用的Index方法來添加一個視圖模板。要做到這一點,在Index方法中單擊滑鼠右鍵,然後單擊“ 添加視圖“

出現添加視圖對話方塊。保留預設值,並單擊添加按鈕:

您可以在方案總管中看到MvcMovie\HelloWorld檔案夾和已被建立的MvcMovie\View\HelloWorld\Index.cshtml檔案:

顯示了已被建立的Index.cshtml檔案:

在<h2>標籤後面添加以下HTML。

<p>Hello from our View Template!</p>

完整的MvcMovie\HelloWorld\Index.cshtml檔案如下所示。

@{
    ViewBag.Title = "Index";

}

<h2>Index</h2>

<p>Hello from our View Template!</p>

註:如果您使用的是Internet Explorer 9,您將看不到在上面用黃色高亮標記的<p>Hello from our View Template!</p>,單擊“相容性檢視”按鈕, 在IE瀏覽器中,表徵圖會從變為純色的表徵圖。另外,您也可以在Firefox或Chrome查看本教程。

如果您正在使用Visual Studio 2012,在方案總管中,按右鍵Index.cshtml檔案,並選擇“ 在頁面檢查器中查看“。

 

完整文檔下載:Asp.Net MVC4入門指南.pdf

--------------------------------------------------------------------------------------------------------------------

譯者註:

本系列共9篇文章,翻譯自Asp.Net MVC4 官方教程,由於本系列文章言簡意賅,篇幅適中,從一個樣本開始講解,全文最終完成了一個管理影片的小系統,非常適合新手入門Asp.Net MVC4,並由此開始開發工作。9篇文章為:

1. Asp.Net MVC4 入門介紹

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2012/11/01/2749906.html

2. 添加一個控制器

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-controller

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2012/11/02/2751015.html

3. 添加一個視圖

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-view

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2012/11/06/2756711.html

4. 添加一個模型

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2012/12/17/2821495.html

5. 從控制器訪問資料模型

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/accessing-your-models-data-from-a-controller

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/01/11/2855935.html

6. 驗證編輯方法和編輯檢視

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-edit-methods-and-edit-view

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/01/24/2874622.html

7. 給電影表和模型添加新欄位

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-new-field-to-the-movie-model-and-table

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/02/26/2933105.html

8. 給資料模型添加校正器

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/03/05/2944030.html 

9. 查詢詳細資料和刪除記錄

· 原文地址:http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/examining-the-details-and-delete-methods

· 譯文地址:http://www.cnblogs.com/powertoolsteam/archive/2013/03/07/2948000.html

10.第三方控制項Studio for ASP.NET Wijmo MVC4 工具應用

 

· 地址:http://www.cnblogs.com/powertoolsteam/archive/2013/05/09/3068699.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.