如何在视图中显示列表变量?

How can I display a list variable in a view?

问题

为什么我的视图模型不能遍历我的 List Books 变量?

代码

模型定义

public class Shelf
{
    public int bookId { get; set; }
    public string Name { get; set; }
    public string Notes { get; set; }
    public List<Book> Books { get; set; }
}

public class Book
    {
        public int BookId { get; set; }
        public string Name { get; set; }
        public string Notes { get; set; }
        public Shelf shelf { get; set; }
    }

public class ReadViewModel
    {
        public int ShelfId { get; set; }
        public string Name { get; set; }
        public List<Book> Books { get; set; }
    }

控制器似乎没问题,因为我能够显示 ReadViewModel 的 ShelfId 和 Name。 编辑我这里有应网友要求的动作方法

 [HttpGet]
    public ViewResult Details(int ShelfId)
    {
        //find the right building for edit
        var shelfs = from shelf in applicationDBContext.Shelfs
                                         where shelf.ShelfId == ShelfId
                                         select shelf;

        ReadViewModel readViewModel = new ReadViewModel
        {
            ShelfId = shelfs.First().ShelfId,
            Name = shelfs.First().Name,
            Books = shelfs.First().Books, //Error still present despite this
        };
        return View("Details", readViewModel);
    }

我知道,我应该在上面使用 async 和 await,但我正在自学 mvc、vnext 等。所以在我使用更多之前使用上面教我流程如何工作 "best practices".

查看

 @model Application.Model.ReadViewModel
    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @{
        ViewBag.Title = "Shelf Details";
    }


<h1>Building Details</h1>
<table>

        <tr>
            <td>@Html.LabelFor(model => model.ShelfId)</td>
            <td>@Html.DisplayTextFor(model=>model.ShelfId)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(model => model.Name)</td>
            <td>@Html.DisplayTextFor(model => model.Name)</td>
        </tr>
     </table>
<h2>Rooms</h2>
<table>
    <tr>
        <td>@Html.LabelFor(model => model.Books.First().BookId)</td>
        <td>@Html.LabelFor(model => model.Books.First().Name)</td>
    </tr>

    @*@foreach (var book in Model.Books)
    {

    }*@

    @*@for(var booksIndex = 0; booksIndex < Model.Books.Count(); booksIndex++)
    {
        <tr>
            <td>@Html.DisplayTextFor(model => model.Books[booksIndex].BookId)</td>
            <td>@Html.DisplayTextFor(model => model.Books[booksIndex].Name)</td>
            <td></td>
        </tr>
    }*@
</table>

问题

当我尝试使用 for 或 foreach 遍历房间时,我不断收到 Value cannot be nullObject instance cannot be null 之类的错误。 如果我注释掉循环,我可以显示货架 ID 和名称。

尝试次数

这对我来说没有意义。有一次我没有上架书籍的数据,所以我认为这就是问题所在。所以我输入了一些数据,但仍然得到完全相同的错误。所以我确信我的观点无法遍历我的书并列出它们。

在我尝试通过浏览器访问视图之前,上面给出的两个循环都没有错误。

是的,控制器很重要,数据的绑定方式也很重要。你如何填充模型?你使用什么框架?完整的,Linq2SQL,小巧玲珑?

您得到 null 或未设置对象的原因显然是因为未设置这些值。没有人试图欺骗你。

该项目可以正常编译,因为代码语法在构建时是正确的。在视图中出现错误(称为运行时错误)的原因是绑定的数据不正确或丢失。编译器无法验证运行时数据...因为它对此一无所知。

使用 Visual Studio 您可以在视图中的 @code 部分设置断点。我建议在第一个循环处设置一个断点。单击最左边的边缘以获得红点。

运行 调试 (F5) 和 Visual Studio 中的站点将在视图的断点处停止。然后您可以检查 ModelList<Model> 并查看那里有什么数据。确保填充了所需的属性。 (图中显示了运行调试后的断点。出现一条黄线,您可以将鼠标悬停在变量上以检查值或右键单击它们并将它们添加到观察面板)

这是您在运行时遇到这些错误的唯一原因。