Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.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# 这是错误的吗?为什么';不是吗?ASP.NET MVC视图包_C#_Asp.net_Asp.net Mvc_Views - Fatal编程技术网

C# 这是错误的吗?为什么';不是吗?ASP.NET MVC视图包

C# 这是错误的吗?为什么';不是吗?ASP.NET MVC视图包,c#,asp.net,asp.net-mvc,views,C#,Asp.net,Asp.net Mvc,Views,下面是我显示客户信息的代码示例: <table class="table table-striped jambo_table bulk_action"> <thead> <tr class="headings"> <th class="column-title">Address </th> <th class="column-title">Title</th>

下面是我显示客户信息的代码示例:

<table class="table table-striped  jambo_table bulk_action">
   <thead>
      <tr class="headings">
         <th class="column-title">Address </th>
         <th class="column-title">Title</th>                                            
      </tr>
   </thead>
   <tbody>
    @foreach (var customer in ViewBag.Customers)
    {
     <tr>
      <td>@customer.Code</td>
      <td>@customer.DefaultName.Name</td>
     </tr>
    }
   </tbody>
</table>

地址
标题
@foreach(ViewBag.Customers中的var客户)
{
@客户代码
@customer.DefaultName.Name
}
正如你们所看到的,我正在通过
ViewBag
循环显示我表格中的一些数据, 我在
ViewBag.Customer
中没有数据时测试了它,它没有抛出任何异常, 为什么
foreach
没有在空列表上抛出异常

我想我会得到这样的东西:

对象引用未设置为对象的实例

如果我尝试在没有数据的情况下循环查看
ViewBag.Customer


为什么会这样?

Foreach
循环调用
GetEnumerator
方法。如果枚举数为空,则不会抛出错误(即:它只是退出循环)


如果集合为null,则此方法调用将导致
NullReferenceException
,因为它无法获取枚举数。在这种情况下,控制器必须返回一个空列表才能不抛出错误。

正如@hsoesanto所说,IEnumerable调用GetEnumerator,GetEnumerator返回IEnumerator的实例,但如果集合为空,则不会抛出任何异常

这是ICollection的原始代码

[__DynamicallyInvokable]
public IEnumerator<T> GetEnumerator()
{
  return this.items.GetEnumerator();
}
[[uuu\u DynamicallyInvokable]
公共IEnumerator GetEnumerator()
{
返回此.items.GetEnumerator();
}
但是,如果深入到实现ICollection的列表的实现,当它返回列表的枚举数时,您会看到对列表大小的检查

[__DynamicallyInvokable]
[Serializable]
public struct Enumerator : IEnumerator<T>, IDisposable, IEnumerator
{
   ....

  [__DynamicallyInvokable]
  public bool MoveNext()
  {
    List<T> list = this.list;
    if (this.version != list._version || (uint) this.index >= (uint) list._size)
      return this.MoveNextRare();
    this.current = list._items[this.index];
    this.index = this.index + 1;
    return true;
  }
[[uuu\u DynamicallyInvokable]
[可序列化]
公共结构枚举器:IEnumerator、IDisposable、IEnumerator
{
....
[[uuuu动态调用可禁用]
公共图书馆
{
List=this.List;
如果(this.version!=列表。_version | |(uint)this.index>=(uint)列表。_大小)
返回这个.MoveNextRare();
this.current=列表项[此.index];
this.index=this.index+1;
返回true;
}
正如你在这条线上看到的

如果(this.version!=list.| | |(uint)this.index>=(uint)list._size)

这意味着,当您尝试在空元素集合上迭代时,MoveNext返回false,然后foreach结束


因此,回到您的ViewBag,如果客户不为null,那么您不应该收到任何错误或异常。首先,编译器在运行时检测到您正在ViewBag上使用元素集合,如果它不为null,那么它将在该集合上迭代,调用GetEnumerator返回IEnumerator并调用MoveNext和Current以获取列表中的每个元素。

显示您的控制器类。整个类。如果它是空列表,则不会抛出错误。如果它为null,则应该有错误(我认为)。为什么不使用ViewModel将所有属性传递给强类型视图?我刚刚意识到,从数据库返回客户列表的控制器,在没有项目的情况下,会意外地返回空列表。因此,是的,您的答案是正确的,žlist不是空的,它是空的。。