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就可以协作完成显示页面与逻辑处理的工作了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值