Application of MVC mode in ASP.NET

zhaozj2021-02-17  58

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);} / //

/// Required method for Designer support -. do not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this.Load = new System . EventHandler (this.page_load);} #ENDREGON}} view here has the corresponding class fragment we have the ASPX page and its support. I used 2 ASPX pages in the example. 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"%> View1 </ title> </ head> <body> <body> <body> <body> <body> <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"</p> <p>Columns = "32" maxlength = "32" /> </ td> </ tr> <tr Valign = "middle"> <td class = "field"> second Number </ td> <td> <ask: TextBox ID = "Two" runat = "server"</p> <p>Columns = "16" Maxlength = "16" /> </ td> </ tr> <tr> <TD colspan = "2" align = "center"> <ask: button id = "btnsubmit" runat = "server" Text = "add" onclick = "submit" cssclass = "button" /> </ td> </ tr> </ table> </ form> </ body> </ html></p> <p>This is the code of View1.aspx.cs ---------------------------------------------------------------------- -----------</p> <p>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;</p> <p>Using mvctest;</p> <p>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 ///mods the address and Sends to the next aspx page. public class view1: controller {</p> <p>/////////////////////////////////////////////////////////////////////////////// '</p> <p>/////////////////////////////////////////////////////////////////////////// i '</p> <p>Protected textbox one; protected textbox.</p> <p>Protected HTMLFORM FORM; protected button btnsubmit;</p> <p>Public view1 () {serviceId = 1;}</p> <p>/// get the number to be added from the user, perform the Operation /// and send the result. Protected void Submit (Object sender, system.eventargs e) {string first = one.text String second = two.text;</p> <p>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);}}</p> <p>Private void Page_load (Object Sender, System.EventArgs E) {}</p> <p>#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);} / // <summary> /// Required method for Designer support -. do not modify /// the contents of this method with the code editor /// </ summary> private void InitializeComponent () {this.Load = new System .Eventhandler (this.page_load);} #ENDREGON}}</p> <p>The following is the original file of View2.aspx ----------------------</p> <p>This page gets the result as a request parameter and displays it.</p> <p><% @ 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> <ask: label id = "label1" runat = "server" > </ asp: label> <form id = "View2" method = "post" runat = "server"> </ form> </ body> </ html> Conclusion I hope this example will help you understand the MVC model Architecture. The example code provided here is merely how to implement this model architecture, and in actual applications usually use a configuration file to replace this fixed GetServerPage (), I have chosen this just to keep the code. Simple.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-29348.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="29348" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.048</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '_2F1jFvBExjXk0AtShCXoZkLJGXkg4BSYNQqZLjJ5g7W4ya6BW8xztcjB35NNRgl244xDbX6V_2FP5qpWj1u0vxKYQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>