Asp.net Mvc Framework 三 (Controller與View)

來源:互聯網
上載者:User
這節我們讓Asp.netMVC真正的跑起來
我們自己建立一個新的Controller
開始行動:
在Controllers中建立一個MVC Controller Class,個人宣傳一下.就叫EiceController
附註一下,這裡是個純廣告,沒有興趣可略過此行:www.eice.com.cn為您建立Web2.0社交網站
預設產生的程式碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
    /**//// <summary>
    /// 記不記得前面講過的,所有Controller都要繼承於
    /// Controller類
    /// </summary>
    public class EiceController : Controller
    {
        public void Index(string id) {

        }
    }
}

當然,除了Controller我們還要建個View
先在Views中建個Eice檔案夾
然後我們要建個Index.aspx
注意了:要建MVC View (Content) Page,如果你要使用母板頁就選用Content Page,反之選用一般Page即可
MVC的Aspx檔案與傳統的WebForm的Aspx檔案有所不同

我們將EiceController的Index寫為        public void Index(string id) {
            ViewData["qs"] = id;
            RenderView("Index");
        }

在View即/Views/Eice/Index.aspx中寫內容<asp:Content ID="Content1" ContentPlaceHolderID="MainContentPlaceHolder" runat="server">
<%=ViewData["qs"] %>

</asp:Content>

接下來我們訪問
/eice/index/helloeice
也許你會發現,在頁面上出現了helloeice
由上面兩段程式可以看出
string id用於接收QueryString["id"] 其實Action中的參數除了能接收QueryString以外也是可以接收Forms的
這裡不做過多說明了,在後文中會有介紹
ViewData是一個頁面間的IDictionary用於Controller向View傳遞資料
這樣View與Controller就可以協作完成顯示頁面與邏輯處理的工作了

Asp.net Mvc Framework 系列

相關文章

聯繫我們

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