MVCApplication.zipmvctest.zip
Introduction to an example of a MVC model that demonstrates ASP.NET after searching for a long time. So I realized a good to understand
Simple instance of the MVC model.
For MVC in a traditional application, a single code is to handle 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.
Correct data in the model. More knowledge about MVC can be connected below http://www.uidesign.net/1999/papers/webmvc_part1.html
The model assumes that you know the MVC model, I want to give you this example will demonstrate how to implement MVC mode in ASP.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 buildin. // ip is a managed library trata will be reference by /// Your Web Application
Public class model {public model () {} // static method for adding two number @ @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 - number int c - result public static int add (string a, string b) {int c = int32.parse a) int32.parse (b);
Return C;
}
The controller is 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
Various 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 page in your site // it is represented as a name value pair. public enum viewpages {view1 = 1, view2, view3}
Public int serviceId {get {return;
Set {id = value;}
}
/// @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 Method Override Protected Void OnLoad (Eventargs E) {}
Private void Page_load (Object Sender, System.EventArgs E) {}
#Region Web Form Designer generated code override protected void oninit (Eventargs e) {//// Codegen: this call is required by the asp.net web form designer. // initializecomponent (); base.onit (e);} / //
Here is the original code of View1.aspx page --------------------------------- <% @ page language = "c #" codebehind = "view1.aspx.cs" autoeventwireup = "false"
Inherits = "mvcapplication.view1"%>