Home

Vici MVC Quick Walkthrough - Part 2 - Looping

We'll take our class from the previous example, and show some tabular data:

[Layout("master")]
[View("home")]
public class Home : Controller
{
   public class Employee
   {
       public Employee(string name, decimal salary) { Name = name; Salary = salary }
 
       public string Name;
       public decimal Salary;
   }
 
   public void Run()
   {
      List<Employee> employees = new List<Employee>();
 
      employees.Add( new Employee("Mark Jones" , 65000) );
      employees.Add( new Employee("John Doe" , 83000) );
      employees.Add( new Employee("Phil Baxter" , 125000) );
 
      ViewData["Employees"] = employees;
   }
}

home.htm

<html>
<body>
<table>
<tr><th>Name</th><th>Salary</th></tr>
<!--{{ foreach employee in Employees }}-->
<tr><td>{{ employee.Name }}</td><td>{{ employee.Salary }}</td></tr>
<!--{{ endfor }}-->
</table>
</body>
</html>

Pretty simple, isn't it?

The output will be:

NameSalary
Mark Jones65000
John Doe83000
Phil Baxter125000

HTML:

<html>
<head><title>My first Vici MVC page</title></head>
<body>
<table>
<tr><th>Name</th><th>Salary</th></tr>
<tr><td>Mark Jones</td><td>65000</td></tr>
<tr><td>John Doe</td><td>83000</td></tr>
<tr><td>Phil Baxter</td><td>125000</td></tr>
</table>
</body>
</html>

Want to format the numbers a little?

home.htm

<html>
<body>
<table>
<tr><th>Name</th><th>Salary</th></tr>
<!--{{ foreach employee in Employees }}-->
<tr><td>{{ employee.Name }}</td><td>{{ employee.Salary ` #,##0.00 }}</td></tr>
<!--{{ endfor }}-->
</table>
</body>
</html>

Final output:

NameSalary
Mark Jones65,000.00
John Doe83,000.00
Phil Baxter125,000.00

HTML:

<html>
<head><title>My first Vici MVC page</title></head>
<body>
<table>
<tr><th>Name</th><th>Salary</th></tr>
<tr><td>Mark Jones</td><td>65,000.00</td></tr>
<tr><td>John Doe</td><td>83,000.00</td></tr>
<tr><td>Phil Baxter</td><td>125,000.00</td></tr>
</table>
</body>
</html>

>> Next: Part 3