将值设置为模型实例 - ArgumentOutOfRangeException
Set value to model instance - ArgumentOutOfRangeException
我在为模型实例设置值时遇到错误
控制器
var operatingDTO = new List<OperatingDTO>();
operatingDTO[0].Id = 1;
型号
public class OperatingDTO
{
public int Id { get; set; }
}
错误
'operatingDTO[0].Id' threw an exception of type 'System.ArgumentOutOfRangeException'
您应该实例化该元素,然后将其添加到列表中。有一个完整的指南:Object and Collection Initializers (C# Programming Guide).
C# lets you instantiate an object or collection and perform member assignments in a single statement.
替换为
var operatingDTO = new List<OperatingDTO>();
operatingDTO.Add(new OperatingDTO { Id = 0 });
甚至
var operatingDTO = new List<OperatingDTO> { new OperatingDTO { Id = 0 } };
我在为模型实例设置值时遇到错误
控制器
var operatingDTO = new List<OperatingDTO>();
operatingDTO[0].Id = 1;
型号
public class OperatingDTO
{
public int Id { get; set; }
}
错误
'operatingDTO[0].Id' threw an exception of type 'System.ArgumentOutOfRangeException'
您应该实例化该元素,然后将其添加到列表中。有一个完整的指南:Object and Collection Initializers (C# Programming Guide).
C# lets you instantiate an object or collection and perform member assignments in a single statement.
替换为
var operatingDTO = new List<OperatingDTO>();
operatingDTO.Add(new OperatingDTO { Id = 0 });
甚至
var operatingDTO = new List<OperatingDTO> { new OperatingDTO { Id = 0 } };