ProMesh.net试用(3)-条件语句

本文介绍了一个使用条件判断在模板中显示不同内容的例子。通过在模板中加入特定标签,可以根据数据的不同展示不同的HTML内容。
如果要对输出结果进行条件判断,可以在模板中使用<!--$[if ...]-->标签:

Home.cs:
 1[Layout("master")] [View("home")]
 2public class Home : PageController
 3{
 4   public class Employee
 5   {
 6       public Employee(string name, decimal salary) { Name = name; Salary = salary; }
 7 
 8       public string Name;
 9       public decimal Salary;
10   }

11 
12   public void Run()
13   {
14      List<Employee> employees = new List<Emplyee>();
15 
16      employees.Add( new Employee("Mark Jones" , 65000) );
17      employees.Add( new Employee("John Doe" , 244000) );
18      employees.Add( new Employee("Phil Baxter" , 125000) );
19 
20      ViewData["Employees"= employees;
21   }

22}

23

模板Home.htm:
 1<html>
 2<body>
 3<table>
 4  <tr><th>Name</th><th>Salary</th></tr><th>&nbsp;</th>
 5  <!--$[foreach employee in Employees]-->
 6    <tr>
 7       <td>$[employee.Name]</td>
 8       <td>$[employee.Salary]</td>
 9       <td><!--$[if employee.Salary > 200000]-->Rich Guy!<!--$[endif]--></td></tr>
10  <!--$[endfor]-->
11</table>
12<p>There are $[Employees.Count] employees</p>
13</body>
14</html>
15

结果:
NameSalary 
Mark Jones65000
John Doe244000Rich Guy!
Phil Baxter125000

There are 3 employees


对应的Html:
 1<html>
 2<head><title>My first ProMesh.NET page</title></head>
 3<body>
 4<table>
 5  <tr><th>Name</th><th>Salary</th><th>&nbsp;</th></tr>
 6  <tr><td>Mark Jones</td><td>65000</td><td></td></tr>
 7  <tr><td>John Doe</td><td>244000</td><td>Rich Guy!</td></tr>
 8  <tr><td>Phil Baxter</td><td>125000</td><td></td></tr>
 9</table>
10<p>There are 3 employees</p>
11</body>
12</html>
13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值