Quantcast
Channel: MEPO Forum - 程式設計
Viewing all articles
Browse latest Browse all 99

Learn MVC Project in 7 days – Day 2 – Lab 5 - Understand strongly typed Views (no replies)

$
0
0
=======================================================


Learn MVC Project in 7 days – Day 2


=======================================================

Lab 5 - Understand strongly typed Views
Lab 3 – 了解 強型別 View

=======================================================

目的:
(一)了解 Action Method 與 View 之間 傳遞 資料 的其中一種方法:model


=======================================================


步驟:(詳見原文)

(一) 將 View 設定成 強型別( strongly typed )

在 View 的最上面 加上下列敘述

@model WebApplication1.Models.Employee


這個使得 View 成為一個 型別 Employee 的 強型別 View ( strongly typed view )


(二)在 View 裡面 使用 變數資料

此時,如果在 View 裡面打上 @Model 再接著打上 點(.),智能系統就會自動列出 Model ( 也就是 Employee Class ) 的所有 property.

https://www.codeproject.com/KB/aspnet/897559/G.png

鍵入下列程式碼 (不好意思,這裡好像沒辦法打 html 指令)

Employee Details

Employee Name : @Model.FirstName @Model.LastName 

@if(Model.Salary>15000)
{

        Employee Salary: @Model.Salary.ToString("C")

}
else
{           

       
        Employee Salary: @Model.Salary.ToString("C")

}

(三) 將 Model 的資料從 Action Method 傳到 View

鍵入下列程式碼:

Employee emp = new Employee();
emp.FirstName = "Sukesh";
emp.LastName="Marla";
emp.Salary = 20000;           
return View("MyView",emp);

(四)測試結果:

https://www.codeproject.com/KB/aspnet/897559/H.png

=======================================================

討論:

(一)

Viewing all articles
Browse latest Browse all 99

Trending Articles