ASP. NET MVC Learning (i)

Source: Internet
Author: User

MVC design pattern breaks down applications into 3 main parts: Model, view, controller (models, views, controllers)

Model-models represent a series of classes used to describe business logic, such as business models and data access operations, as well as data models. It also defines the business rules for how data is processed.

View-Views represent UI parts like CSS, jquery, HTML, and so on. Its primary responsibility is to demonstrate acceptance of data or models from the controller.

Controller-the Director is responsible for processing incoming requests. It accepts the user's input through the view, then processes the data model entered by the user, and finally renders the result to the user through the view.

I. Create an ASP. NET MVC Project

(1) Open new project, "File", Visual Studio point.

(2) Select the Web application, enter the project name, select the storage path, click OK.

(3) Select the MVC template

(4) Select Change authentication (changing authorization), select "No Authentication" in the pop-up dialog and click OK.

(5) Create controller, in Explorer, right click on Controller folder, select Add->controller (Controller)

Enter the name of the controller "TestController" and click Add.

In this step, pay special attention to not deleting the "Controller" keyword in the name.

(6) Creating behavioral methods

Open the newly created TestController class to discover the generated index method and change the output:

(7) Run and test press F5 key, in the address bar as "controllername/actionname" in the form of input, you need to pay attention to the input controller name, you cannot enter "controller" only enter "Test".

Ii. Supplementary Questions

(1) The Action (behavior) method is a controller built-in method of the public type, capable of receiving and processing the user's request, in the above example, the index method returns a response of a string type. The non-public method cannot be called in the web. If we need other functions to accomplish certain functions, we can use the action method property to decorate.

(2) View is associated with a controller placed in a specific directory. This particular folder is named "Controllername" and placed in the View folder, and all controllers are available if the view is placed in the shared folder.

(3) The View and controller of ASP. NET MVC are not strictly matched, one action method can refer to multiple view, and a view can be used by an action approach.

(4) Table of relationships between ActionResult subclasses

(5) Data transfer from controller to view can be done via ViewData and ViewBag

ViewBag:

The viewbag is a viewdata dynamic wrapper, which is equivalent to the encapsulation process on the basis of ViewData.

Definition of ViewBag:

Definition of ViewData:

The difference between the two:

A.viewdata is a dictionary type, the assignment is in dictionary mode, the corresponding value,viewdata["MyName" is read by the key value.

B.viewbag is a dynamic type that can be used to assign values directly to a property, Viewbag.myname

C.viewdata and ViewBag are valid only in the current action, and values in Viewviewdata and viewbag can be accessed by each other

Values in D.viewdata and viewbag can be accessed by each other

Disadvantages of both:

A. Performance issues; The values in ViewData are object types that must be cast to the appropriate type before they are used. Additional performance burdens will be added.

B. There is no compile-time error without type safety, and if you attempt to convert it to the wrong type, the runtime will give an error. Good programming experience tells us that errors must be captured at compile time.

C. There is no proper connection between data transmission and data reception; in MVC, controllers and view are loosely connected. The controller is unable to capture the view changes and the view cannot capture the changes that have occurred inside the controller. A viewdata or ViewBag value is passed from the controller, and when the developer is writing to the view, it must record what value is about to be obtained from the controller. If the controller is not the same developer as the view developer, the development effort becomes very difficult. Can cause many run-time problems and reduce development efficiency.

(6) View strong type

This approach enables data transfer, but it violates the basic principles of MVC. According to Mvc,v is a view-only UI and does not contain any logical layers.

(7) ViewModel is an implicitly-declared layer in an ASP. NET MVC application. It is used to maintain data transfer between model and view, which is the data container of view.

How the work Works

A.controller handles user interaction logic or simple judgments. Handling user Needs

B.controller get one or more model data

C.controller decide which view best matches the user's request

D.controller will create and initialize the ViewModel object based on the model data and view requirements.

E.controller ViewModel data to a view, such as ViewData or viewbag or a strongly typed view.

F.controller return to view.

(8) Identity authentication and authorization

ASP. NET MVC Learning (i)

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.