Danh mục tài liệu

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

Số trang: 46      Loại file: pptx      Dung lượng: 1.27 MB      Lượt xem: 17      Lượt tải: 0    
Xem trước 5 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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 Studio.NET.
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ài liệu được xem nhiều:

Tài liệu có liên quan: