
Bài giảng Lập trình web: View - 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: View - Nguyễn Hà Giang ViewNguyen Ha Giang 1 Objectives• Define and describe views• Explain and describe the razor engine• Define and describe the HTML helper methods 2 Working with Views• To display HTML content to the user, you can instruct the controller in the app to return a view.• A View – Provides the UI of the app to the user – Is used to display content of an app and also to accept user inputs – Uses model data to create this UI – Contains both HTML markup and code that runs on the Web server 3 View Engines• Are part of the MVC Framework that convert the code of a view into HTML markup that a browser can understand• Are divided in the following two categories: – Web Form view engine: Is a legacy view engine for views that use the same syntax as ASP.NET Web Forms. – Razor view engine: Is the default view engine starting from MVC 3. This view engine does not introduce a new programming language, but instead introduces new markup syntax to make 4Specifying the View for an Action• 1/8 While creating an ASP.NET MVC app, you often need to specify a view that should render the output for a specify action• When you create a new project in VS.NET, the project by default contains a View directory• In an app, if a controller action returns a view, your app should contain the following: – A folder for the controller, with the same name as the controller without the Controller suffix 5Specifying the View for an Action• 2/8 Following code shows an Index action that returns an ActionResult object through a call to class public the View() method HomeController of the Controller : Controller { class: public ActionResult Index() { return View(); } } 6Specifying the View for an Action• 3/8 VS 2013 simplifies the process of creating a view• You can create the view file by performing the following steps: – Right-click inside the action method for which you need to create a view. – Select Add View from the context menu that appears. The Add View dialog box is displayed, as shown in the following figure: 7Specifying the View for an Action• Click Add, VS automatically4/8 creates an appropriate directory structure and adds the view file to it.• Following figure shows the view file that VS.NET creates for the Index action method of the HomeController class in the Solution Explorer window: 8Specifying the View for an Action• 5/8 In the Index.cshtml file, you can add the following code that the view should display: My Test View Welcome to the Website • This code creates a view with a title and a9Specifying the View for an Action• 6/8 When you access the Index action of the HomeController from a browser, the Index. cshtml view will be displayed.• You can use the following URL to access the Index action of the HomeController: http:///Home /Inde x http:// 10Specifying the View for an Action• 7/8 view from an action You can also render a different method.• To return a different view, you need to pass the name of the view as a parameter.• Following code shows how to return a different view: public class HomeController : Controller { public ActionResult Index() { return View(AnotherPage); } }• This code will search for a view inside the /Views/Home 11Specifying the View for an Action• 8/8 MVC app, you might also While developing an ASP.NET need to render a view that is present in a different folder instead.• To render such view, you need to specify the path to the view. public class HomeController : Controller { public ActionResult Index() { return View(~/Views/Demo/Welcome.cshtml); } }• This code will display the view, named Welcome.cshtml 12 Passing data 1/14• In an ASP.NET MVC app, a controller typically performs the business logic of the app and needs to return the result to the user through a view• You can use following objects to pass data between controller and view: – ViewData – ViewBag – TempData 13 Passing data 2/14• ViewData – Passes data from a controller to a view – The life of a ViewData object exists only during the current request – The value of ViewData becomes null if the request is redirected – ViewData requires typecasting when you use complex data type to avoid error Vie wData[]=; : Is a string value to identify the object present in ViewData : Is the object present ...
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 Working with views View engines HTML helper methods Passing dataTà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