I'm trying to insert to a table from excel using LinqToExcel and when I call
ValidateModel(u);
It throws an exception:
Cannot convert from System.DateTime to System.Array
I'm using DataAnnotations on the viewmodel.
Datetime in ViewModel:
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime FECHA_ADQ { get; set; }
Controller:
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)
{
// ...
}
}
Datetime in excel file:
This is what it looks like on the debugger:
I couldn't resolve the issue with excel so i just ended up validating after the mapping to the base model and it worked.
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.