我试图使用CSVhelper插件来读取上传的CSV文件.这是我的modelBinder类:
public class SurveyEmailListModelsModelBinder : DefaultModelBinder { public override object BindModel(ControllerContext controllerContext,ModelBindingContext bindingContext) { var csv = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); var file = ((csv.RawValue as HttpPostedFileBase[]) ?? Enumerable.Empty<HttpPostedFileBase>()).FirstOrDefault(); if (file == null || file.ContentLength < 1) { bindingContext.ModelState.AddModelError( "","Please select a valid CSV file" ); return null; } using (var reader = new StreamReader(file.InputStream)) using (var csvReader = new CsvReader(reader)) { return csvReader.GetRecords<SurveyEmailListModels>().ToArray(); } } }
这些是我要映射到的对象:
public class SurveyEmailListModels { [Key] [CsvField(Ignore = true)] public int SurveyEmailListId { get; set; } [CsvField(Index = 0)] public int ProgramId { get; set; } [CsvField(Index = 1)] public virtual SurveyProgramModels SurveyProgramModels { get; set; } [CsvField(Index = 2)] public string SurveyEmailAddress { get; set; } [CsvField(Index = 3)] public bool SurveyResponded { get; set; } }
> base {“在访问数据之前必须先在读取器上调用read”.)CsvHelper.CsvHelperException {CsvHelper.CsvReaderException}
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。