MVC学习笔记4 认识View和Controller

本文介绍如何在ASP.NET MVC中创建Controller和View,并实现二者之间的数据交互。通过具体步骤指导读者新建Controller,设置Action方法,创建并配置View文件,以及如何在URL中传递参数到Controller再传递到View进行展示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先我们自己新建一个新的Controller

在Controllers上点右键-添加-Controller,选项

mvc

 

mvc2

VS 自动生成代码:这里我把Controller Name 更改为TestController

        //
        // GET: /Test/

        public ActionResult Index()
        {
            return View();
        }

 

然后,新建View文件。

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

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

在对应的Controller的Action中点右键,选择Add View。

mvc

NEXT:

mvc2

接下来是VS的工作了:

<%@ 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">

</asp:Content>

这样好像View和Controller都建好了,可是现在他们还是不能协调工作。

更改Controller

        //
        // GET: /Test/

        public ActionResult Index(string id)
        {
            ViewData["id"] = id;
            return View();
        }

更改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>Index的ID参数的值是:<%=ViewData["id"] %></h2>

</asp:Content>
OK,运行了。
访问 http://localhost:9230/Test/Index/123123 看看效果。
 

MVC在URL的驱动下,通过Global.asax.cs

   routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

来获取到id参数的值。

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

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

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

--此函数默认返回汉字拼音的首字母,第二个参数不为空则返回全拼。 create or replace function f_getFirstOrFullSpell(p_cnStr In varchar2,p_sign In number default null) return varchar2 as lv_spell varchar2(200); lv_temp Varchar2(10); lv_char varchar2(10); --lv_bytes varchar2(100); li_bytes Integer; --li_pos Integer; begin if p_cnStr is null then return ''; end if; for i In 1..length(p_cnStr) loop lv_char:=substr(p_cnStr,i,1); if lengthb(lv_char) = 1 then lv_spell:=lv_spell||lv_char; elsif lengthb(lv_char) = 2 then --Select replace(substrb(dump(lv_char,1010),instrb(dump(lv_char,1010),'ZHS16GBK:')),'ZHS16GBK: ','') Into lv_bytes from dual; --li_pos:=instr(lv_bytes,','); --li_bytes:=substr(lv_bytes,1,li_pos-1)*256+substr(lv_bytes,li_pos+1)-256*256; Select ascii(lv_char)-256*256 Into li_bytes From dual; select max(spell) Into lv_temp from table(f_getSpellcode) where code<=li_bytes; if p_sign is null then lv_spell:=lv_spell||substr(lv_temp,1,1); else lv_spell:=lv_spell||lv_temp; end if; elsif lengthb(lv_char) = 3 then --Select replace(substrb(dump(convert(lv_char,'ZHS16GBK','UTF8'),1010),instrb(dump(convert(lv_char,'ZHS16GBK','UTF8'),1010),'UTF8:')),'UTF8: ','') --Into lv_bytes from dual; --li_pos:=instr(lv_bytes,','); --li_bytes:=substr(lv_bytes,1,li_pos-1)*256+substr(lv_bytes,li_pos+1)-256*256; Select ascii(lv_char)-256*256 Into li_bytes From dual; select max(spell) Into lv_temp from table(f_getSpellcode) where code<=li_bytes; if p_sign is null then lv_spell:=lv_spell||substr(lv_char,1,1); else lv_spell:=lv_spell||lv_char; end if; end if; end loop; return lv_spell; end; --hanjs,07-10-24,此函数默认返回汉字拼音的首字母,第二个参数不为空则返回全拼。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值