locked
Using c# lists in webmatrix razor RRS feed

  • Question

  • User-1370183403 posted

    I have some data in the form of  'name','value' that i need to turn into an array or list, so that i can loop through the array or list to produce a chart.

    I have no asp.net background so I am learning razor. My research suggests that a c# list seems to be the best way to go as I do not know how many  'name','value' pairs i will be handling.

    However, I cannot work out how to do this as all the examples are c# Here is one i found on the net at http://www.c-sharpcorner.com/UploadFile/camurphy/csharpLists03302006170209PM/csharpLists.aspx

    public class Person
    
    {
    
              public int age;
    
              public string name;
    
              public Person(int age, string name)
    
              {
    
                       this.age = age;
    
                       this.name = name;
    
              }
    
    } 
    
    We can create a list of Person objects and add six people like so: 
    
    List<person>people = new List<person>(); 
    
    people.Add(new Person(50, "Fred"));
    people.Add(new Person(30, "John"));
    people.Add(new Person(26, "Andrew"));
    people.Add(new Person(24, "Xavier"));
    people.Add(new Person(5, "Mark"));
    people.Add(new Person(6, "Cameron")); 
    

    How would I modify this so i can achieve this in razor?

    cheers



    Wednesday, September 18, 2013 6:23 PM

Answers

  • User-908601221 posted

    Hi,

    Put your Person class in a .cs file called Person.cs in the App_Code directory.

    Then put the rest of your code in the .cshtml file where you want to display the data.

    Remember that C# is case sensitive so you need to use Person with a capital P if that is what you call your class.  Also Razor and C# are two different things.  Razor is the stuff that uses @{ } and you put that in your markup along with HTML.  C# is a language that can be used with Razor.  You can put C# code in the @{ } areas.

    Actual code:

    App_Code/Person.cs:

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    /// <summary>
    /// Summary description for Person
    /// </summary>
    public class Person
    {
        public int age;
        public string name;
    
        public Person(int age, string name)
        {
                this.age = age;
                this.name = name;
        }
    
    }

    Person.cshtml:

    @{
        List<Person>people = new List<Person>();
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            @{
                foreach (Person p in people){
                    <div>@p.age, @p.name</div>
                }
            }
            
        </body>
    </html>
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2013 7:15 PM
  • User281315223 posted

    You can actually write C# code directly within a Razor View as long as you use the appropriate syntax around it : 

    @{
        //Create your Class
        List<Person> people = new List<Person>();
    
        //Build your collection
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    
        //Iterate through your collection
        foreach(var person in people)
        {
             //Do something here
        }
    }

    or if you wanted to declare your list seperately, you could still use it later within your View as seen below : 

    @{
        //Create your Class
        List<Person> people = new List<Person>();
    
        //Build your collection
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    }
    
    
    <body>
        <!-- Build a list of your people -->
        <ul>
             @foreach(var person in people){
                 <b>@String.Format("{0} (Age : {1})",person.name, person.age)</b>
             }
        </ul>
    </body>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2013 7:33 PM

All replies

  • User-908601221 posted

    Hi,

    Put your Person class in a .cs file called Person.cs in the App_Code directory.

    Then put the rest of your code in the .cshtml file where you want to display the data.

    Remember that C# is case sensitive so you need to use Person with a capital P if that is what you call your class.  Also Razor and C# are two different things.  Razor is the stuff that uses @{ } and you put that in your markup along with HTML.  C# is a language that can be used with Razor.  You can put C# code in the @{ } areas.

    Actual code:

    App_Code/Person.cs:

    using System;
    using System.Collections.Generic;
    using System.Web;
    
    /// <summary>
    /// Summary description for Person
    /// </summary>
    public class Person
    {
        public int age;
        public string name;
    
        public Person(int age, string name)
        {
                this.age = age;
                this.name = name;
        }
    
    }

    Person.cshtml:

    @{
        List<Person>people = new List<Person>();
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    }
    
    <!DOCTYPE html>
    
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title></title>
        </head>
        <body>
            @{
                foreach (Person p in people){
                    <div>@p.age, @p.name</div>
                }
            }
            
        </body>
    </html>
    



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2013 7:15 PM
  • User281315223 posted

    You can actually write C# code directly within a Razor View as long as you use the appropriate syntax around it : 

    @{
        //Create your Class
        List<Person> people = new List<Person>();
    
        //Build your collection
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    
        //Iterate through your collection
        foreach(var person in people)
        {
             //Do something here
        }
    }

    or if you wanted to declare your list seperately, you could still use it later within your View as seen below : 

    @{
        //Create your Class
        List<Person> people = new List<Person>();
    
        //Build your collection
        people.Add(new Person(50, "Fred"));
        people.Add(new Person(30, "John"));
        people.Add(new Person(26, "Andrew"));
        people.Add(new Person(24, "Xavier"));
        people.Add(new Person(5, "Mark"));
        people.Add(new Person(6, "Cameron"));
    }
    
    
    <body>
        <!-- Build a list of your people -->
        <ul>
             @foreach(var person in people){
                 <b>@String.Format("{0} (Age : {1})",person.name, person.age)</b>
             }
        </ul>
    </body>



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 18, 2013 7:33 PM
  • User-1370183403 posted

    Thanks that helped a lot

    Monday, September 23, 2013 12:58 AM