ZLYPERSON (translation) Keyword MVC mode ASP.NET home http://www.codeproject.com/Useritems/mvcasp.asp mvcapplication.zipmvctest.zip introduction After searching for a long time but can't find a demonstration ASP.NET Example of the MVC model. So I implemented a very good example of the MVC model. About the MVC in a traditional application, a single code to process all things. With the MVC model, you can organically divide some parties for three collaboration: models, views, and controllers. The view is part of the user who is used directly. It formats the data to show the data on the screen in various forms. However, it is actually, it does not contain data. Data is included in the model. Finally, the controller part accepts the user's operation command to correct the data within the model. More knowledge about MVC can be connected to the following http://www.uidesign.net/1999/papers/webmvc_part1.html model assumes that you know the MVC model, I want to give you this example will demonstrate how to be on the ASP MVC mode in .NET. The model is where all your business logic codes are located. I will give a class implementation two numbers and send the results back to the user interface. 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 applicationpublic class Model {public Model () {} / / static method for adding two numbers /// @Params Int A, B - Numbers to be added @return c - resultpublic 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 - resultpublic static int add (string a, string b) {int c = int32.parse a) int32.parse (b); return c;}}} Controller In ASP.NET, you usually request services directly. Replacing a service as a sub-controller, it is the main entry point for us to enter the ASP.NET application. The location of a center that is not available here judges whether you have permission to use the service, or no other handleable processing should usually occur in all services. However, allowing all classes associated with the ASPX to inherit a universal system.web.ui.page subclass, you can place all of these pre-processed code to overlocked the online class () method. OnLoad () will be called when the page is loaded at each page. Calling the Page_Load () method in your class associated with the ASPX code, you can make you as a central location verification service is allowed to use and do some redirects, here you can use server.transfer () as true The central controller is the same. I named my view Control Class Controller. Once you have to verify login information before displaying any ASPX page or do some initial operations for the user, then these can be combined into the OnLoad () method. Now, when you want to redirect or connect to an ASPX page in your application, you will use the GetServicePage () method to get the name of the file you want to jump. If you have renamed your website to reorgan your website, you will immediately find out far-reaching maintenance work.
If you want you to rename an ASPX file, now you can rename the file and change the names of GetServicePage, so your website can continue to work.
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;. namespace MVCApplication {/// This class acts as a controller extends System.Web.UI.Page This class /// maintains all the information about your web pages and performs any pre-requiste /// conditions.public class Controller: System.Web.UI.Page {private int id = 0; // enum ViewPages has the information of all the aspx pages in your site // it is represented as a name value pair.public Enum viewpages {view1 = 1, view2, view3} public int serviceId {get {return;}} /// method buying for getting the name of the point by provike /// ITS REference Number. /// @param int page_id - a number to retrieve the page name./// @return string page - name of the aspx page.public string GetServicePage (int page_id) {// Get the page name by passing the number from enum string page = Enum.GetName (typeof (ViewPages), page_id); return page;} // Could implement pre-requiste condition in this methodoverride protected void OnLoad (EventArgs e) {} private void Page_Load (object sender, System.EventArgs e ) {} # region Web Form Designer generated code (Eventargs e) {//// codegen: this call is required by the asp.net web form designer./initializecomponent();Base.onit (E); } ///
One is two numbers that are entered for obtaining the user to use two numbers, and the other is returned to the user. Here is the original code of View1.aspx page --------------------------------- <% @ page language = "c #" codebehind = "view1.aspx.cs" autoeventwireup = "false" inherits = "mvcapplication.view1"%>
Namespace mvcapplication {// this is our view in the mvc model. this class is a sub class of controller.///////////////mods the add method in ////mods the address 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 Controllerprivate int Next_Screen = 2; /// The screen to. send on the case of failure / error ./// Id of the screen as is in the Controllerprivate 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.issalid) {string first = one .Text; string second = two.text; int sum = model.add (first, second); // Get the page_path = getServicePage (next_screen); server.transfer (Page_pa) ".? Aspx sum =" th sum.ToString ());} else {// On failure direct to this screenServer.Transfer (GetServicePage (Failure_Screen));}} private void Page_Load (object sender, System.EventArgs e) {} #Region Web Form Designer Generated CodeOverride Protected Void OnNit (Eventargs E) {//// Codegen: This Call is Required by The ASP.NET Web Form Designer./initializationComponent();base.onit (ie); ///
} #endregion}} The following is the original file of View2.aspx ---------------------- this page gets the result as a request parameter and displays it. <% @ Page language = "c #"%> <% @ 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 "%>