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



PLEASE DISABLE ADBLOCKER AND SUPPORT US!

Please support us by enabling ads on this page. Refresh

YOU DON'T LIKE ADS, WE ALSO DON'T LIKE ADS!
 
But we have to show ads on our site to keep it free and updated. We have to pay huge server costs, domain costs, CDN Costs, Developer Costs, Electricity and Internet Bill. Your little contribution will encourage us to regularly update this site.