I have tried to upload a csv file to my application but this not showing all rows, I am geting only one row here is my code
Model
public class FileUploadViewModel
{
public string Name { get; set; }
public string School { get; set; }
public string Age { get; set; }
public string DOB { get; set; }
public string ParentName { get; set; }
}
Controller
[HttpPost]
public ActionResult AddparcelView(FileUploadViewModel filedata)
{
var attachedFile = System.Web.HttpContext.Current.Request.Files["CsvDoc"];
if (attachedFile == null || attachedFile.ContentLength <= 0) return Json(null);
var csvReader = new StreamReader(attachedFile.InputStream);
var uploadModelList = new List<FileUploadViewModel>();
string inputDataRead;
var values = new List<string>();
var uploadModelRecord = new FileUploadViewModel();
var jsonToOutput = "";
while ((inputDataRead = csvReader.ReadLine()) != null)
{
values.Add(inputDataRead.Trim().Replace(" ", "").Replace(",", " "));
}
values.Remove(values[0]);
values.Remove(values[values.Count - 1]);
var i = 0;
foreach (var value in values)
{
i++;
var eachValue = value.Split(' ');
uploadModelRecord.Name = eachValue[0] != "" ? eachValue[0] : string.Empty;
uploadModelRecord.School = eachValue[1] != "" ? eachValue[1] : string.Empty;
uploadModelRecord.Age = eachValue[2] != "" ? eachValue[2] : string.Empty;
uploadModelRecord.DOB = eachValue[3] != "" ? eachValue[3] : string.Empty;
uploadModelRecord.ParentName = eachValue[4] != "" ? eachValue[4] : string.Empty;
//jsonToOutput = JsonConvert.SerializeObject(uploadModelRecord, Formatting.Indented);
Console.WriteLine(i);
}
return Json(uploadModelRecord);
}
I have five rows at this CSV file but i getting only one row like this
Age: "sdfdsf"
DOB: "test"
Name:"mohammad2"
ParentName:"test"
School:"32"
So this is just one row data I am showing up with json, but i not getting the full json array with other 4rows please help me, Thanks