Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表中的值为空。ArgumentNullException:值不能为null。(参数&x27;源&x27;)_C#_Asp.net Core - Fatal编程技术网

C# 列表中的值为空。ArgumentNullException:值不能为null。(参数&x27;源&x27;)

C# 列表中的值为空。ArgumentNullException:值不能为null。(参数&x27;源&x27;),c#,asp.net-core,C#,Asp.net Core,我想从创建页面返回到数据库中的索引页面,但它仍然没有任何数据。 这是索引页中的代码: @if (Model.Books.Count() > 0 ) { <table class="table table-bordered table-striped"> <tr class="table-secondary"> <th>

我想从创建页面返回到数据库中的索引页面,但它仍然没有任何数据。
这是索引页中的代码:

@if (Model.Books.Count() > 0 )
    {
        <table class="table table-bordered table-striped">
            <tr class="table-secondary">
                <th>
                    <label asp-for="Books.FirstOrDefault().Name"></label>
                </th>
                <th>
                    @Html.DisplayNameFor(a => a.Books.FirstOrDefault().Author)
                </th>
                <th>
                    <label asp-for="Books.FirstOrDefault().ISBN"></label>
                </th>
                <th>

                </th>
            </tr>
            @foreach (var data in Model.Books)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(a => data.Name)
                    </td>
                    <td>
                        @Html.DisplayFor(a => data.Author)
                    </td>
                    <td>
                        @*using html helper to display the data*@
                        @Html.DisplayFor(a=>data.ISBN)
                    </td>
                    <td>
                        <button class="btn btn-success btn-sm">Edit</button>
                        <button class="btn btn-danger btn-sm">Delete</button>
                    </td>
                </tr>
            }
        </table>
    }
    else
    {
        <p class="text-center text-danger">No Book Available.<br /><b>Please create a book !!</b></p>
    }
@if(Model.Books.Count()>0)
{
@DisplayNameFor(a=>a.Books.FirstOrDefault().Author)
@foreach(Model.Books中的var数据)
{
@DisplayFor(a=>data.Name)
@DisplayFor(a=>data.Author)
@*使用html助手显示数据*@
@DisplayFor(a=>data.ISBN)
编辑
删除
}
}
其他的
{

没有可用的书。
请创建一本书

}
当它直接从主页转到索引页时,它将转到
else
语句,但从创建页转到索引时,会发生错误

我想知道如何处理此异常或此错误?
我认为没有任何虚拟数据而仅仅为了知识而开发是不现实的。

有什么想法吗?

当model为null或model.Books为null时,您的第一个if语句给出了null引用异常

更改
@if(Model.Books.Count()>0)


当model为null或model.Books为null时,您的第一个if语句给出null引用异常

更改
@if(Model.Books.Count()>0)


您必须检查@if(Model!=null&&Model.Books.Count()>0),以便在从其他页面导航时,如果模型未填充,则以下代码集不会运行。如果Books为null,Model.Books.Count()将引发异常。使用Model?.Books?.Count()无效抛出异常tanks@J.Hsu代码有效,我注意到您必须检查@if(Model!=null&&Model.Books.Count()>0),因此当从其他页面导航时,如果模型未填充,以下代码集不会运行。如果Books为null,Model.Books.Count()将抛出异常。使用Model?.Books?.Count()来避免抛出ExceptionTanks@J.Hsu。我注意到,代码是有效的
 @if (Model?.Books?.Count() > 0 )