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


Asp.net Mvc Framework 系列
posted @ 2008-03-10 23:47 重典 阅读(2889) 评论(8)  编辑 收藏 网摘 所属分类: ASP.NET MVC

  回复  引用  查看    
#1楼2008-03-13 14:00 | miao~      
呵呵...~~~好神奇...看下一张去了...又不会的再问你啊...
  回复  引用  查看    
#2楼2008-07-16 10:18 | 云の世界      
重典兄:
RenderView("Index");
这种情况,rander的
/Views/ControlerName/Index.aspx
但是,如果我想利用view实现换肤的话,
如何做呢。如果这样,似乎很有问题;
string Theme = "blue";
RenderView(Theme + "Index");
那这样在每个/Views/ControlerName/里,都要保含所有版本的view.
通常应该每个主题一个文件夹阿。

  回复  引用  查看    
#3楼2008-07-16 10:25 | 云の世界      
asp.net mvc,对于view的存储位置限制太死了
如果作为默认倒也无可厚非。但是不提供
自定义的设定的方法的话,就太缺陷了。

  回复  引用  查看    
#4楼[楼主]2008-07-16 10:28 | 重典      
@云の世界
皮肤嘛,一般更改CSS就可以了,如果是换模板的话可以
string[] themes=["blue","red"...];
public void index(string theme){
if(themes.Contains(theme))
RenderView(theme + "Index");
else
RenderView("Index");
}
类似这样.也可以
RenderView("/" + theme + "/Index");
即,/blue/index
这样管理比较方便

  回复  引用  查看    
#5楼[楼主]2008-07-16 10:34 | 重典      
@云の世界
其实我倒感觉还是挺灵活的,只是一开始在整个文件夹内四处存,现在收到了更小的一个文件夹里,感觉....
只是感觉吧,其实和原来的WEB方式都是都在一个文件夹里嘛
另外,你存成什么格式,他都可以自动找到,相对来说是更宽松了

  回复  引用  查看    
#6楼2008-07-16 15:42 | 云の世界      
邹兄,你说的,好像还是这个结构吧。
[views]
.|
.---[Controler1]
...|
...---[blue]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Red]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
.---[Controler2]
...|
...---[blue]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
...---[Red]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
这样,主题view无法独立出来,不利于分离主题,管理主题。
而我觉得,最理想的结构是
[views]
.|
.---[blue]
...|
...---[Controler1]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Controler2]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx
.---[Red]
...|
...---[Controler1]
.... |
.... ---index.aspx
.... |
.... ---Edit.aspx
...---[Controler2]
.... |
.... ---hello.aspx
.... |
.... ---report.aspx




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1099679




相关文章:

相关链接: