如何使用 Webforms 应用程序中的通用列表中的数据填充 HTML table?

How can I populate an HTML table with data from a generic list in a Webforms app?

根据 Goliath-slayer 的回答 ,我尝试了以下方法将通用列表中的数据放入 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>

...但我得到的是:

在更多上下文中,这里是 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
    }

}

...以及 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;
        }

    }
}

我是什么人 missing/misapplying/misunderstanding 让我看不到这些数据?

从你的输出来看,你的 Razor 代码似乎没有被解释。 This question 处理如何在 ASP 表单应用程序中使用 Razor 语法:

如果您有一个简单的页面(没有表单控件),您可以只输入 .cshtml.vbhtml 扩展名,语法应该有效。

对于更复杂的场景,您可以考虑使用 RazorEngine,但您无法从 ASP.NET MVC 获得完整的 Razor 引擎的所有优点。