Welcome! 登入 註冊
美寶首頁 美寶百科 美寶論壇 美寶落格 美寶地圖

Advanced

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

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


[url=https://www.codeproject.com/articles/897559/learn-mvc-in-days-day]Learn MVC Project in 7 days – Day 2[/url]


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

[url=https://www.codeproject.com/articles/897559/learn-mvc-in-days-day#Lab5-Understand strongly typed Views]Lab 5 - Understand strongly typed Views[/url]
Lab 3 – 了解 強型別 View

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

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


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


步驟:(詳見原文)

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

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

[code]
@model WebApplication1.Models.Employee
[/code]


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


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

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

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

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

[code]
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")

}
[/code]

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

鍵入下列程式碼:

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

(四)測試結果:

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

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

討論:

(一)



Edited 4 time(s). Last edit at 01/31/2017 02:28AM by RandomVariable.
(編輯記錄)