using System; namespace MVCTest { /// This class is where we have the business logic build in. /// It is a managed library that will be referenced by /// your web application public class Model { public Model() { } // static method for adding two numbers // // @params int a,b - numbers to be added // @return c - result public static int Add(int a, int b) { int c = a + b; return c; } // static nethod to add two numbers // // @params string a,b - numbers to be added // @return int c - result public static int Add(string a, string b) { int c = Int32.Parse(a) + Int32.Parse(b); return c; } } }
Controller:
在ASP.Ner中,你通常会直接访问服务端。代替服务端是一个潜在的Controller,它是主要的进入口。这将不提供的是一个中控中心,如果你首先想要使用服务端,或者任何的公共处理过程将会再发生在所有的服务端。然而,你所有的code-behind的页面都继承子一个公共的类那就是System.Web.UI.Page,你可以把所有的初始化的代码都放在OnLoad()的事件中,这是重载自每当页面在任何时候被读取的时候都会被框架进行调用的页面类的OnLoad()。现在,在你的代码的任何地方,你都可以重新定位并连接到一个新的aspx页面。你可以通过调用getServicePage()的方法来获得将要跳转的页面的名字。如果你想要重命名一个aspx页面文件,你就可以马上修改该文件的名字,并且改变getServicePage那你
的站点就仍然可以工作
视图
这部分是我们所有的aspx页面并且和它所需要的对aspx进行支持的code-behind类。这下面的例子中,我写了2个aspx页面,一个用来得到用户的输入进而可以计算书2个数字的和,而另一个则输出结果。这是View1.aspx页面的:
<%@ Page language="c#" Codebehind="View1.aspx.cs" AutoEventWireup="false" Inherits="MVCApplication.View1" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <html> <head> <title>View1</title> </head> <body MS_POSITIONING="GridLayout"> <h3>ADDITION</h3> <form runat="server" ID="Form" method="post"> <table> <tr valign="middle"> <td class="field">First Number</td> <td> <asp:textbox id="one" runat="server" columns="32" maxlength="32" /> </td> </tr> <tr valign="middle"> <td class="field">Second Number</td> <td> <asp:textbox id="two" runat="server" columns="16" maxlength="16" /> </td> </tr> <tr> <td colspan="2" align="center"> <asp:button id="btnSubmit" runat="server" text="Add" onClick="Submit" cssclass="button" /> </td> </tr> </table> </form> </body> </html>
这是View1.aspx.cs的code-behind类
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using MVCTest; namespace MVCApplication { /// This is our View in the MVC model. /// This class is a sub class of Controller. /// This class gets the input from the View1.aspx /// page, calls the add method in /// Model class and sends to the next aspx page. public class View1 : Controller { /// The next screen to send on the case of success. /// Id of the screen as is in the Controller private int Next_Screen = 2; /// The screen to send on the case of failure/error. /// Id of the screen as is in the Controller private int Failure_Screen= 3; protected TextBox one; protected TextBox two; protected HtmlForm Form; protected Button btnSubmit; public View1() { ServiceId = 1; } /// Get the number to be added from the user, perform the operation /// and send the result. protected void Submit(object sender, System.EventArgs e) { if(Page.IsValid) { string first = one.Text; string second = two.Text; int sum = Model.Add(first, second); //Get the page name from the Controller string page_path = GetServicePage(Next_Screen); Server.Transfer(page_path+".aspx?sum="+ sum.ToString()); }else { //On failure direct to this screen Server.Transfer(GetServicePage(Failure_Screen)); } } private void Page_Load(object sender, System.EventArgs e) { } } }
下面是View2.aspx页面:
此页通过一个请求参数得到结果并且显示它。
<%@ Page language="c#" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.SessionState" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Web.UI.HtmlControls" %> <script language="C#" runat="server"> void Page_Load(Object sender, EventArgs e) { Label1.Text = Request.Params.Get("sum"); } </script> <html> <head> <title>View2</title> </head> <body MS_POSITIONING="GridLayout"> <h3>SUM</h3> <asp:Label id="Label1" Runat="server"></asp:Label> <form id="View2" method="post" runat="server"> </form> </body> </html> Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=19046
| 第1页: 第1页 |