This article provides an advanced overview of ASP. net mvc models, views, and controllers. In other words, it explains "M" "V" "C" in ASP. net mvc ". After reading this article, you should understand ASP.. Net MVC, you should also know how ASP. net MVC application and ASP.. NET web form applications.
Note that homecontroller has two Method (Function): Index () and about (). These two methods correspond to the two behaviors exposed by the controller. The URL/home/index calls homecontroller. index () method, URL/home/about call
Homecontroller. About () method.
All public methods in the controller are exposed as controller actions, which means that you can call these methods by typing the corresponding URL in the browser address bar.
Understanding View
The homecontroller class exposes Two Controller actions: Index () and about (), which return a view. The view includes HTML Tag and content sent to the browser. Applications A view is equivalent to a page.
You must create your View , Homecontroller. Index () returns the view in the following path:
Viewshomeindex. aspx
Homecontroller. About () returns the view in the following path:
Viewshomeabout. aspx
Generally, if you want to return a view for the Controller action, you must create a sub-folder with the same name as the controller in the view folder. In this sub-folder, you must also create an action with the same name as the controller action. aspx file.
The content of the about View File is displayed in listing 3.
Listing 3 about. aspx
<%@ Page Language="VB" MasterPageFile="~/Views/Shared/
Site.Master"
AutoEventWireup="false" CodeBehind="About.aspx.vb" Inherits="MvcA
pplication1.About" %>
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainC
ontent" runat="server">
<h2>About Us</h2>
<p>
TODO: Put <em>about</em> content here.
</p>
</asp:Content>
If you ignore the first line in listing 3 and the rest of the view is standard HTML, you can add any required HTML content to it.
View and ASP page or ASP. net web forms are very similar. Views can include HTML content and scripts, which you can use. NET Programming Language (such as C # Or VB.. Net) write scripts and use scripts to display dynamic content, such as database data.
Understanding Models
We have already discussed controllers and views. The last topic we want to discuss is model. What is MVC model?
The MVC model includes all the logic of the application. The logic does not exist in the view and controller. The model should include all the business logic and database access logic of the application, for example, if you access your database using LINQ to SQL, you should create a class (dbml file) from LINQ to SQL under the model (models) folder ).
The view should only include User For interface-related logic, the Controller should only include the minimum logic that is exposed to the request to return the correct view or the logic that redirects the user to another controller, any other logic should be included in the model.
Generally, you should be the Controller Weight Loss To fatalize the model, the Controller method should only contain a small amount of code. If the Controller behavior is too bloated, you should think about how to migrate the implicit logic to the model.
Summary
This article introduces the differences between different parts of ASP. net mvc, and Learning How to route the inbound request to the appropriate controller behavior through URL routing, and how the view is returned to the browser, finally, it introduces the business logic and database access logic that the model should contain.
Original article: understanding models, views, and controllers by Stephen Walther