How to use Collection inside Razor Syntax

In this chapter you will learn
  • How to use Collection inside Razor Syntax
  • Programming Example

How to use Collection inside Razor Syntax

A collection is a group of objects of the same type. There are mainly two common collections are used in razor, Array and Dictionary. You have learned Array in previous chapter so we will focus on Dictionary in this chapter.

Dictionary

A dictionary is a collection of key/value pairs and you can use it in programming as follow:

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        @{
            var result = new Dictionary<string, int>();
            result.Add("Jack", 75);
            result.Add("Steven", 80);
            result.Add("Clark", 95);
        }
        <h3>Results of the students are:</h3>
        <h3>Jack : @result["Jack"]</h3>
        <h3>Steven : @result["Steven"]</h3>
        <h3>Clark : @result["Clark"]</h3>
    </body>
</html>
  Output
Results of the students are:
Jack : 75
Steven : 80
Clark : 95

Summary

In this chapter you learned to use Dictionary in razor syntax. You are free to use any types of collection in razor syntax. In the next chapter you will learn creating Methods with parameter in Razor Syntax.

More Articles

 

Share your thought