ASP. net mvc Tutorial: understanding models, views, and controllers (5) (end)

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.