ASP.NET MVC案例教程(基於ASP.NET MVC beta)——第二篇:第一個頁面

來源:互聯網
上載者:User

讓第一個頁面跑起來

現在,我們來實現公告系統中的第一個頁面——首頁。它非常簡單,只包括所有公告分類的列表, 並且每個清單項目是一個超連結。其中分類資料是用我們的Mock組件得到的。實現後介面如下:

在開始之前,我們要刪幾個東西。因為預設情況下建立一個MVC項目時裡麵包含了幾 個樣本頁面,我們要做的就是:

1.將Controllers檔案夾下所有檔案刪除。

2.將Views檔案夾下除了Shared檔案夾和Web.config 外的所有檔案刪除,然後將Shared檔案夾裡面的檔案刪除。

完成以上幾步後,就可以開始實現第一個頁面了。

實現控制器

在Controllers檔案夾下建立一個檔案,類型選擇“MVC Controller Class”,名字命名為HomeController.cs。這就是一個 控制器類。然後我們為它編碼,具體代碼如下:

HomeController.cs:

1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Web;
5using System.Web.Mvc;
6using System.Web.Mvc.Ajax;
7using MVCDemo.Models;
8using MVCDemo.Models.Interfaces;
9using MVCDemo.Models.Entities;
10
11namespace MVCDemo.Controllers
12{
13  public class HomeController : Controller
14  {
15    public ActionResult Index()
16    {
17      ICategoryService cServ = ServiceBuilder.BuildCategoryService();
18      ViewData["Categories"] = cServ.GetAll();
19       return View("Index");
20    }
21  }
22}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.