1

Based on an answer here by Goliath-slayer, I tried the following to get data from my generic list into an html table:

<table width="100%" id="my-grid" class="table table-condensed">
    <thead>
        <tr>
            <th>Title</th>
            <th>Category</th>
            <th>Year</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var book in booksList)
        {
            <tr>
                <td>@book.Title</td>
                <td>@book.Category</td>
                <td>@book.PublishYear</td>
            </tr>
        }
    </tbody>
</table>

...but all I get is this:

enter image description here

In more context, here is the class:

namespace HTML5AndBootstrap4Sandbox
{
    public class SandboxData
    {
        public string Title { get; set; }
        public string Category { get; set; }
        public int PublishYear { get; set; } // int so that it can be ordered
    }

}

...and the hydration of an instance of a generic list of the class:

namespace HTML5AndBootstrap4Sandbox
{
    public class DataUtils
    {
        protected IList<SandboxData> booksList { get { return GetSandboxData(); } }

        private List<SandboxData> GetSandboxData()
        {
            List<SandboxData> sbdList = new List<SandboxData>();

            SandboxData sbd = new SandboxData();
            sbd.Title = "The Innocents Abroad";
            sbd.Category = "Nonfiction";
            sbd.PublishYear = 1869;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Roughing It";
            sbd.Category = "Nonfiction";
            sbd.PublishYear = 1872;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "A Tramp Abroad";
            sbd.Category = "Nonfiction";
            sbd.PublishYear = 1880;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Life on the Mississippi";
            sbd.Category = "Nonfiction";
            sbd.PublishYear = 1883;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Following the Equator";
            sbd.Category = "Nonfiction";
            sbd.PublishYear = 1897;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "King Leopold's Soliloquy";
            sbd.Category = "Essay";
            sbd.PublishYear = 1905;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "The Gilded Age: A Tale of Today";
            sbd.Category = "Novel";
            sbd.PublishYear = 1873;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "The Adventures of Tom Sawyer";
            sbd.Category = "Novel";
            sbd.PublishYear = 1876;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "The Prince and the Pauper";
            sbd.Category = "Novel";
            sbd.PublishYear = 1881;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Adventures of Huckleberry Finn";
            sbd.Category = "Novel";
            sbd.PublishYear = 1884;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "A Connecticut Yankee in King Arthur's Court";
            sbd.Category = "Novel";
            sbd.PublishYear = 1889;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "The American Claimant";
            sbd.Category = "Novel";
            sbd.PublishYear = 1892;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Pudd'nhead Wilson";
            sbd.Category = "Novel";
            sbd.PublishYear = 1894;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "Extracts from Adam's Diary";
            sbd.Category = "Short Story";
            sbd.PublishYear = 1904;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "The War Prayer";
            sbd.Category = "Short Story";
            sbd.PublishYear = 1905;
            sbdList.Add(sbd);

            sbd = new SandboxData();
            sbd.Title = "To the Person Sitting in Darkness";
            sbd.Category = "Essay";
            sbd.PublishYear = 1901;
            sbdList.Add(sbd);

            return sbdList;
        }

    }
}

What am I missing/misapplying/misunderstanding that's preventing me from seeing this data?

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

1 Answers1

2

From you output, it looks that your Razor code is not interpreted. This question deals with how to use Razor syntax within a ASP forms application:

If you have a simple page (no forms controls) you can just put .cshtml or .vbhtml extension and the syntax should work.

For more complex scenarios you may consider working with RazorEngine, but you will no get all the goodies of the full fledged Razor Engine from ASP.NET MVC.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164