What is MVC?
The full name of MVC is Model View Controller, which is an abbreviation of model-view-controller. It is a model of software design. It organizes code by a method of separating business logic, data, and interface display. The business logic is aggregated into one component. While improving and personalizing the custom interface and user interaction, there is no need to rewrite the business logic. MVC has been uniquely developed to map traditional input, processing, and output functions into a logical graphical user interface structure.
MVC framework
- MVC originally existed in desktop programs, where M was the business model and V was the
- MVC is a pattern for creating Web applications using MVC (Model View Controller): [1]
- MVC refers to a framework of the MVC pattern, which compulsively enables application input, processing, and
- Difference between framework and design pattern
- Many programmers often confuse the framework pattern with the design pattern and consider MVC to be a design pattern. In fact they are completely different concepts. [7]
- According to Tianji.com's data, the Web-based MVC framework has experienced unprecedented prosperity in the J2EE world.
- In the ASP.NET MVC framework without its own controls, the page display is completely back to the era of writing HTML code. Fortunately, asp.net mvc framework also comes with two helper classes HtmlHelper and UrlHelper. In addition, some helper classes are also extended in the MvcContrib extension project, so that we can not only use the complete html to write the pages that need to be displayed, we can use these helper classes to complete them, but we still need to generate html code when we run of.
- HtmlHelper class
- The HtmlHelper class is located under the System.Web.MVC.Html namespace. Mainly include FormExtensions, InputExtensions,
LinkExtensions, SelectExtensions, TextAreaExtensions, ValidationExtensions, RenderPartialExtensions and other 7 static classes, they are all implemented using extension methods.
- InputExtensions class: There are five types of extension methods, which are used for CheckBox control, Hidden control, Pass control, RadioButton control, and TextBox control.
- LinkExtensions class: This class is mainly used to generate related links, which mainly extends the ActionLink and RouteLink methods.
- ActionLink: The extension method mainly implements a connection, with a total of ten overloaded methods.
- UrlHelper helper class
- Seeing the class name also knows that this class is used to make URLs in ASP.NET MVC applications.
- UrlHelper provides four very common four methods.
- 1.Action method generates a URL by providing Controller, Action and various parameters,
- 2.Content method is a virtual, relative path to the absolute path of the application,
- 3.Encode method is to encrypt the URL address, the same as Server.Encode method.
- 4.RouteUrl method is to provide URL matching in the routing rules specified in the current application.
- There are two other attributes, namely the RequestContext and RouteCollection attributes, which refer to the two attributes including the HTTP context and RouteData. In addition, RouteCollection is the routing rule specified in the entire current application.
- Custom control
- HtmlHelper provided by Microsoft is enough for most developers to use, but there are some functions to use HtmlHelper provided by Microsoft may not meet the requirements. Let's talk about how to customize the process.
- The first custom method is an extension to the HtmlHelper object.
- Three elements of extension method implementation: 1. Static class 2. Static method 3. This keyword
- 1. Define a class first, for example: MyHtmlHelper:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace MvcApplicationFirstDome.Models {
- // static class
- public static class MyHtmlHelper
- {
- // static method
- }
- }
- 2. Assume that the method to be extended is GetSpan, the effect is that when you pass in the parameters, the result is returned after being encapsulated internally. The code is as follows. Note the use of the System.Web.Mvc namespace in the MyHtmlHelper class.
- // static method
- public static string GetSpan (this HtmlHelper htmlHelper, string text)
- {
- return "<span style = 'color: Red'>" + text + "</ span>";
- }
- After the above two steps, HtmlHelper's extension method GetSpan is basically ready to be used. Next, we will explain how to call it on the page.
- When calling, pay attention to the next namespace: if the namespace of the extension method is different from the namespace of the page, you must reference the namespace of the extension method, otherwise there is no way to call the custom method on the page.
- After referencing the namespace, you can call the custom extension method on the corresponding page.
- For some projects, custom controls are too complex and time-consuming. At this time, you can also download the Mvc control that suits your needs from the technical community or source code station. Such as some control packages, table controls and so on. [13]