C# C“模型验证抛出”;无法从System.DateTime转换为System.Array;

C# C“模型验证抛出”;无法从System.DateTime转换为System.Array;,c#,asp.net-mvc,entity-framework,datetime,data-annotations,C#,Asp.net Mvc,Entity Framework,Datetime,Data Annotations,我正在尝试使用LinqToExcel从excel插入到表中,当我调用 ValidateModel(u); 它抛出一个异常: 无法从System.DateTime转换为System.Array 我正在viewmodel上使用DataAnnotations ViewModel中的日期时间: [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] public DateTime FECHA_

我正在尝试使用LinqToExcel从excel插入到表中,当我调用

ValidateModel(u);
它抛出一个异常:

无法从System.DateTime转换为System.Array

我正在viewmodel上使用DataAnnotations

ViewModel中的日期时间:

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime FECHA_ADQ { get; set; }
控制器:

foreach (var a in excel.Worksheet<ViewModel>(sheet.First())) 
{
    //Validations//
    try 
    {
        Context db = new Context();
        ViewModel u = a;
        ModelState.Clear();
        ValidateModel(u);

        //Insert//
    }
    catch(Exception)
    {
        // ...
    }
}
foreach(excel.Worksheet(sheet.First())中的变量a)
{
//验证//
尝试
{
Context db=newcontext();
视图模型u=a;
ModelState.Clear();
验证模型(u);
//插入//
}
捕获(例外)
{
// ...
}
}
excel文件中的日期时间:

这是调试器上的外观:


我无法用excel解决这个问题,所以我只是在映射到基本模型后进行了验证,结果成功了

ModelState.Clear();

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<ViewModel, Model>();
});

IMapper mapper = config.CreateMapper();
var inv = mapper.Map<ViewModel, Model>(u);

if (inv.FECHA_ADQ.Year <= 2005) {
    errors.AppendLine("Serial: " + a.SERIAL + " Error on Row: " + row + " Cause: Invalid Date.");
    counter++;
} else {
    ValidateModel(inv);
    Context.ModelDB.Add(inv);
    Context.SaveChanges();
}    
ModelState.Clear();
var config=new-MapperConfiguration(cfg=>{
CreateMap();
});
IMapper mapper=config.CreateMapper();
var inv=映射器映射(u);

如果(inv.FECHA_ADQ.Year我无法用excel解决这个问题,所以我只是在映射到基本模型后进行了验证,结果成功了

ModelState.Clear();

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<ViewModel, Model>();
});

IMapper mapper = config.CreateMapper();
var inv = mapper.Map<ViewModel, Model>(u);

if (inv.FECHA_ADQ.Year <= 2005) {
    errors.AppendLine("Serial: " + a.SERIAL + " Error on Row: " + row + " Cause: Invalid Date.");
    counter++;
} else {
    ValidateModel(inv);
    Context.ModelDB.Add(inv);
    Context.SaveChanges();
}    
ModelState.Clear();
var config=new-MapperConfiguration(cfg=>{
CreateMap();
});
IMapper mapper=config.CreateMapper();
var inv=映射器映射(u);

如果(inv.FECHA_ADQ.Year,其中是
ValidateModel()
已定义?@JeroenvanLangen这是
System.Web.Mvc
中的一个方法,它适用于另一个表,我也这么做。@datahook所以听起来像是数据问题,根据您所说的,您能看到两个表之间有什么不同吗?@datahook以防万一,有时可能会有不可见的字符专门用于Microsoft office pro从其他位置复制粘贴数据时的风管source@datahook检查excel中datetime列的数据类型。可能是字符串,并且包含一些无效字符。请尝试将其设为日期类型,其中是
ValidateModel()
已定义?@JeroenvanLangen这是
System.Web.Mvc
中的一个方法,它适用于另一个表,我也这么做。@datahook所以听起来像是数据问题,根据您所说的,您能看到两个表之间有什么不同吗?@datahook以防万一,有时可能会有不可见的字符专门用于Microsoft office pro从其他位置复制粘贴数据时的风管source@datahook检查excel中datetime列的数据类型。可能是字符串,并且包含一些无效字符。请尝试将其设置为日期类型