Asp.net Mvc Framework 三 (Controller與View)

 這節我們讓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就可以協作完成顯示頁面與邏輯處理的工作了
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章