Bài giảng Lập trình web: Models - Nguyễn Hà Giang

The goals of this chapter are: Define and describe models, explain how to create a model, describe how to pass model data from controllers to view, explain how to create strongly typed models, explain the role of the model binder, explain how to use scaffolding in Visual . | Models Nguyen Ha Giang 1 Objectives Define and describe models Explain how to create a model Describe how to pass model data from controllers to view Explain how to create strongly typed models Explain the role of the model binder Explain how to use scaffolding in Visual 2 Introducing Models A model is class containing properties that represents data of an app A model represents data associated with the app MVC Framework is based on the MVC pattern The MVC pattern defines the following three types of models, where each model has specific purpose: Data model: represent classes that iteract with a database. Data models are set of classes that can either follow the database-first approach or code-first app Business model: represent classes that implement a functionality that represents business logic of an application View model: Represent classes that provide information passed between controllers and views 3 Creating a Model To create a model in an app, you need to: Create a public class Declare public properties for each information that the model represents 4 Accessing a Model within a Controller In an MVC app when a user request for some information, the request is received by an action method. The action method is used to access the model storing the data. To access the model, you need to create an object of the model class and either retrieve or set the property values of the object 5 Passing model data from C to V 1/6 Once you have accessed the model within a controller, you need to make the model data accessible to a view so that the view can display the data to the user. To do this, you need to pass the model object to the view while invoking the view You can model the object as follow: A single object A collection of model objects 6 Passing model data from C to V 2/6 In an action method, you can create a model object and the pass the object to a view by using the ViewBag object. In this code, an object of the User model . | Models Nguyen Ha Giang 1 Objectives Define and describe models Explain how to create a model Describe how to pass model data from controllers to view Explain how to create strongly typed models Explain the role of the model binder Explain how to use scaffolding in Visual 2 Introducing Models A model is class containing properties that represents data of an app A model represents data associated with the app MVC Framework is based on the MVC pattern The MVC pattern defines the following three types of models, where each model has specific purpose: Data model: represent classes that iteract with a database. Data models are set of classes that can either follow the database-first approach or code-first app Business model: represent classes that implement a functionality that represents business logic of an application View model: Represent classes that provide information passed between controllers and views 3 Creating a Model To create a model in an app, you .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
377    86    1    18-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.