public ActionResult Index(VehicleSalesModel viewModel) { var builder = new IndexBuilder(viewModel.FileUpload); builder.Build(ModelState, viewModel); return(View(viewModel)); }
// Build the Index view parsing the csv file internal void Build(ModelStateDictionary modelState, VehicleSalesModel viewModel) { if (modelState.IsValid == false) { return; } if (FileUpload != null && FileUpload.ContentLength > 0) { if (FileUpload.FileName.EndsWith(".csv")) { // read file data to string string csvData = StreamToString(FileUpload.InputStream); if (string.IsNullOrEmpty(csvData)) { modelState.AddModelError("File", "File is empty"); return; } // read csv to model viewModel.SalesList = ReadCSVFile(csvData); if (viewModel.SalesList == null || viewModel.SalesList.Count() <= 0) { modelState.AddModelError("File", "File error"); return; } // get vehicle sent most often viewModel.MostSoldVehicle = GetMostSoldVehicle(viewModel.SalesList); } else { modelState.AddModelError("File", "This file format is not supported"); } } else { modelState.AddModelError("File", "Please Upload Your file"); } }