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

本文作者:
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3