Asp.net Mvc 的 Controller与View

我们自己新建一个新的Controller
开始行动:
在Controllers中新建一个MVC Controller Class,个人宣传一下.就叫EiceController

默认生成的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication2.Controllers
{
/**////
/// 记不记得前面讲过的,所有Controller都要继承于
/// Controller类
///
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中写内容

<?xml:namespace prefix = asp />
<%=ViewData["qs"] %>


接下来我们访问
/eice/index/helloeice
也许你会发现,在页面上出现了helloeice
由上面两段程序可以看出
string id用于接收QueryString["id"] 其实Action中的参数除了能接收QueryString以外也是可以接收Forms的
这里不做过多说明了,在后文中会有介绍
ViewData是一个页面间的IDictionary用于Controller向View传递数据
这样View与Controller就可以协作完成显示页面与逻辑处理的工作了