3.Controller与View

 

这节我们让ASP.NET MVC真正的跑起来

一、新建Controller

首先我们自己新建一个新的ControllerControllers上点右键,添加,Controller选项

<!--[if !vml]-->image<!--[endif]--> 
之后出现一个对话框:

<!--[if !vml]-->image<!--[endif]--> 
这里我们将之起名为EiceController
附注一下,这里是个纯广告,无兴趣可略过此行:www.eice.com.cn为您建立Web2.0社交网站
默认生成的代码如下:

    //记不记得前面讲过的,所有Controller都要继承于Controller
    public class EiceController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

二、新建View文件

当然,除了Controller我们还要建个View,先在Views中建个Eice文件夹,然后我们要在其中建个Index.aspx

不过除此之外ASP.NET MVC还为我们提供了一种新建View的快捷方式。

在对应的ControllerAction中点右键,选择Add View

<!--[if !vml]-->image<!--[endif]--> 

之后弹出窗口

<!--[if !vml]-->image<!--[endif]-->

确定好View文件名字及母版文件后点Add就建好了一个View文件。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Index
asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <
h2>Indexh2>
asp:Content>

 

 

如果要建无母版页面勾去select master page即可。

三、编辑ControllerView完成一个简单的页面传值

我们将EiceControllerIndex改写为

    public class EiceController : Controller
    {
        public ActionResult Index(string id)
        {
            ViewData["chsword"] = id;
            return View();
        }
    }

View文件即/Views/Eice/Index.aspx中改写

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Index
asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%=ViewData["chsword"] %>
asp:Content>

下面我们来访问/Eice/Index/HellowEice,可以看到:

<!--[if !vml]-->image<!--[endif]-->

这样我们就将一个值从Url传到Controller,又从Controller传到View显示出来。

由上面程序段可以看出Action的参数string id用于接收{Controller}/{Action}/{id}ID部分

ViewData是一个页面间的IDictionary用于ControllerView传递数据
这样ViewController就可以协作完成显示页面与逻辑处理的工作了

 

那除了ViewData之外我们还有什么方法由ControllerView文件传值?我们除了能显示aspx文件外还能显示其它的文件么?

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值