MVC mode application in ASP.NET

zhaozj2021-02-16  53

MVC mode application introduction: When I experienced a long time in the process of web development, I still didn't find some good code to illustrate the MVC mode development under ASP.NET. I have listed a simple example that gives a good method or process for using MVC. About MVC In traditional applications, a single code handles all things. But with MVC, you can divide your program into part of our work together: Model, View, and Controller (for a unique name, individual thinks unnecessary fully translation, this effect will be better). View is the part you can see, it formats the data and presents before the user's eyes. However, in fact, it does not contain data. The data belongs to Model. Finally, the Controller handles the command submitted and modifies Model. Want to know more about MVC, please open the link below: http://www.uidesign.net/1999/papers/webmvc_part1.html

MODEL: Maybe you know very well for MVC, but I still give a simple code, which shows how to perform MVC in ASP.NET. Model is part of all of the business logic processing. The following class is only two numbers and send the result back. <----------------------------------------------- ---------------------------------> 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 number // @Params Int a, b - numbers to be added @return c - result public static int Add (int A, int b) {int C = a b; returnif

// 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;}}} <------------------------------- -------------------------------------------------> Controller: In ASP.NER, you usually access the server directly. Instead of the server is a potential controller, it is the main entrance port. This will not provide a central control center, if you first want to use the server, or any public processing process will further occur in all the server. However, all your code-behind's pages are inherited. One public class is system.web.ui.page, you can put all the initialized code in an online () event, which is overloaded Whenever the page is read when read at any time, the page class of the page class () is performed. Now, anywhere in your code, you can reposition and connect to a new ASPX page. You can get the name of the page that will be jumped by calling getServicePage (). If you want to rename an ASPX page file, you can immediately modify the name of the file and change GetServicePage, you can still work, this part is the ASPX page we have and it needs to be used. Supported Code-Behind class. In the following example, I wrote 2 ASPX pages, one for obtaining the user's input to calculate 2 numbers, while the other outputs the result.

This is the View1.asp page: <% @ page language = "c #" codebehind = "view1.aspx.cs" autoeventwireup = "false" inherits = "mvcapplication.View1"%> 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> <ask: textbox id =" one " Runat = "server" columns = "32" maxLength = "32" /> </ td> </ tr> <tr Valign = "middle"> <td class = "field"> second Number </ td> <td> <ask: textbox id = "two" runat = "server" 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> This is a code-behind class for View1.aspx.cs: use system Using system.collections; using system.data; using system.drawing; using system.web; using system.Web.SessionState; Using System.Web.ui;</p> <p>Using system.Web.ui.webcontrols; using system.web.ui.htmlcontrols; 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 /// MODEL CLASS 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_path = getServicePage (next_screen); server.transfer (page_path ". Aspx? Sum =" sum.toString ());</p> <p>} else {// on failure Direct to this screen server.transfer (getServicePage (Failure_Screen);}}</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-24147.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="24147" 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.038</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 = 'NEqPUfJUSGMdhtxqATfen2s_2Bv5QdlInj3i65mKnadbCwlbNs2ESwBoiSp7_2BmpuJJQZ451hPaCUXSA9TWx5hq3Q_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>