First, what is it?
MVC, i.e. (Model-view-controller, model-View-controller mode), is similar to layer three, which is used to represent a software architecture pattern. In this mode, the implementation of the system is divided into model models, views view, controller Controlller. Where model: the operation of the database and the general business logic. View: Responsible for making and displaying user interactions; Controller: Handling request and response requests is the mediation of the view layer accessing the model layer.
Second, the concrete realization.
A concrete example of how MVC is implemented
First: Create a new MVC 3 Web application
PS:MVC3 and MVC4, I consulted the relevant information, my understanding is: in the overall architecture and implementation of not much change, change is only one of the specific class or interface changes.
Since the approximate process is to request--→controller--→model and then return to view for display, it is implemented from the bottom up.
1) Model
Create a Login_bs class within the Model folder. Because it is simple to implement, it does not operate on the database, just understand the MVC implementation process and principles. < yo? " /kf/ware/vc/"target=" _blank "class=" Keylink ">vcd4kpha+pgjypgo8l3a+cjxwpjxwcmugy2xhc3m9" Brush:java; " > public class Login_bs {public bool Login (string Username, string password) {if (username = = "1" && password = = "1") {return true;} else {return false;}} }
2) Controllers
Then right-click on the Controllers folder and add the controller
Note: In the front part of the controller is the name of the call to change the control, that is, the form is submitted to login
The specific code is as follows:
123456789101112131415161718192021222324252627 |
<span style=
"font-size:18px;"
> </span><span style=
"font-family:KaiTi_GB2312;font-size:14px;"
>
public class LoginController : Controller
{
//
// GET: /Login/
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
//表单提交方式
public void index(string username, string password)
{
Models.Login_BS loginbs =
new Models.Login_BS();
//username = Request.QueryString["username"];这是get方式提交获取数据
//password = Request.QueryString["password"];
if (loginbs.Login(username, password))
{
Response.Write(
"登陆成功" + username);
}
else
{
Response.Write(
"登录失败" + username);
}
}
}</span><span style=
"font-size:18px;"
>
</span>
|
Then right-click the index () method in which you added the view
After that, under the View folder, there will be a login folder automatically, under the login file, there will be a name called index (add view when you get the name) suffix named cshtml file, this is the controller we added corresponding to the view view. To add specific forms and controls:
The specific code is as follows:
3) View
1234567891011121314151617181920212223 |
<span style=
"font-family:KaiTi_GB2312;font-size:14px;"
>View视图代码:
@{
Layout =
null
;
}
<title>Index</title>
<div>
<form action=
"/Login" method=
"post"
>
<p>用户名:<input type=
"text" name=
"username" data-form-un=
"1500271551930.9514"
></p>
<p>密码:<input type=
"password" name=
"password" data-form-pw=
"1500271551930.9514"
></p>
<p>
<input type=
"submit" value=
"登陆" data-form-sbm=
"1500271551930.9514" style=
"pointer-events: auto;"
></p>
</form>
</div></span>
|
4. Self-understanding
Run the program directly, we will find 404 pages, why does the view view is not displayed? In fact, this involves the problem of the specific division of the MVC pattern. As in the DRP MODEL2 model and the students discussed: strict Model2 mode (MVC) mode JSP directly draw a good page does not exist? Because each page that needs to be displayed must be processed by a servlet before it is drawn by the JSP, that is, without accessing the servlet, there will be no corresponding JSP-drawn HTML page (purely own understanding, if there are different views can be exchanged). Back to ASP, in MVC, the address that the user accesses does not map to the corresponding file in the server, but instead maps to the corresponding actionmethod in the corresponding control, which is determined by Actionmethod to return the user what information. The routing system completes the work of addressing the address of the user to the corresponding action (which can also be the corresponding file), many of which are done automatically by. NET, and the developer needs to tell the. NET user's access address and the specific mapping of the corresponding action. We run the program directly and do not map to the corresponding action method, so it is normal to display the 404 page.
If we want to show a specific page. You need to add the corresponding request access method to the Address bar action method, that is, the address of our form submission/login results can be out.
Three, the comparison three layers:
One obvious difference: relative to the three layer, MVC schema pattern, there is no Aspx.cs file. This is what the MVC model is trying to avoid. Because if the page has code, it naturally contains logic, then it is not MVC. In MVC, the view is the name of the display, just a vector of the content that it wants to display, and what to display, all by the controller. One of the most obvious benefits of this is the decoupling of view and controllers.
The other is the difference in the implementation principle (process):
General ASPX implementations:
and MVC is:
ASP. NET MVC schema pattern