
Bài giảng Lập trình web: Models - Nguyễn Hà Giang
Thông tin tài liệu:
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình web: Models - Nguyễn Hà GiangModelsNguyen 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 Studio.NET 2 Introducing Models• A model is class containing properties that represents data of an app• A model represents data associated with the app• ASP.NET 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 3 Creating a Model• To create a model in an ASP.NET 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 ASP.NET 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 5Passing 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 6Passing 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 class is created and initialized with values. The object is then, passed to the view by using a ViewBag object. 7Passing model data from C to V• 3/6 You can access the data of the model object stored in the ViewBag object from within the view.• In this code, the view accesses and displays the name, address, and email properties of the User model object stored in the ViewBag object 8Passing model data from C to V• 4/6 Following code shows passing a collection of model objects to a view 9Passing model data from C to V 5/6• The preceding code: – Creates and initializes three objects of the model class, named User. – Then, a List collection is created and the model objects are added to it. – Finally, the collection is passed to the view by using a ViewBag object. – Once you pass a collection of model objects to a view using a ViewBag object: • You can retrieve the collection stored in the ViewBag object from within the view. 10Passing model data from C to V• 6/6 Following code snippet shows retrieving model objects from a collection and displaying their properties: 11Passing model data from C to V• Following code snippet6/6 shows passing a collection of model objects to a view as a parameter to the View() method 12Passing model data from C to V • 6/6 This code shows how to retrieve the user information that has been passed to a view by passing a collection of objects as a parameter 13 Using Strong Typing• While passing model data from a controller to a view, the view cannot identify the exact type of the data.• As a solution, you can typecast the model data to a specific type The Mo d e l o b je c tis c as tto the Us e rty p e 14 Using Strong Typing• You can also ignore explicit type c ...
Tìm kiếm theo từ khóa liên quan:
Lập trình web Bài giảng Lập trình web Creating a model Passing model data Using strong typing Strongly types viewsTài liệu có liên quan:
-
161 trang 139 1 0
-
[Thảo luận] Học PHP như thế nào khi bạn chưa biết gì về lập trình?
5 trang 137 0 0 -
Bài giảng Lập trình web nâng cao: Chương 8 - Trường ĐH Văn Hiến
36 trang 124 1 0 -
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 118 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 110 0 0 -
Lập Trình Web: Các trang quản trị trong PHP - GV: Trần Đình Nghĩa
8 trang 108 0 0 -
231 trang 99 1 0
-
101 trang 97 2 0
-
Bài giảng Lập trình web nâng cao: Chương 7 - Trường ĐH Văn Hiến
16 trang 69 1 0 -
Bài giảng Lập trình web bài 2: Làm quen với công cụ vẽ và sử dụng Symbol bảng Library
51 trang 58 0 0 -
Bài giảng Lập trình Web ASP.Net với C#: Chương 9 - Th.S Phạm Đào Minh Vũ
55 trang 56 0 0 -
Lập trình web bằng cơ sở dữ liệu MySQL 5.1 và PHP 5.3: Phần 1
136 trang 47 1 0 -
Tạo mạng xã hội với PHP - part 43
10 trang 46 0 0 -
Bài giảng Thiết kế và lập trình Web - Bài 5: PHP cơ bản
137 trang 44 0 0 -
Giáo trình môn học/mô đun: Lập trình web với ASP.NET: Phần 2
42 trang 43 0 0 -
Hướng dẫn thiết kế template joomla 1.5
10 trang 43 0 0 -
Bài giảng Lập trình web nâng cao: Chương 1 - Trường ĐH Văn Hiến
16 trang 41 1 0 -
Bài giảng Thiết kế và lập trình Web - Bài 8: PHP - MySQL
59 trang 40 0 0 -
Bài giảng Lập trình Web ASP.Net với C#: Chương 6 - Th.S Phạm Đào Minh Vũ
31 trang 40 0 0 -
Bài giảng Lập trình Web: Chương 2 - Ths. Trần Phi Hảo
54 trang 40 0 0