0

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

1 Answer 1

1

You are using the same instance uploadModelRecord in every iteration of your loop. In the first iteration it gets the values of the first line. In the second iteration those values get overwritten with the values of the second line. And so on.

Create a new instance in each iteration and add them to a list. Return the serialized list.

public ActionResult AddparcelView(FileUploadViewModel filedata)
{
    ...
    //Remove line for variable uploadModelRecord. 
    var uploadModelRecords = new List<FileUploadViewModel>(); //List instead of single element.
    foreach (var value in values)
    {
        i++;
        var eachValue = value.Split(' ');
        var uploadModelRecord = new FileUploadViewModel(); //Variable for current line.
        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);
        uploadModelRecords.Add(uploadModelRecord);  //Here you add the variable for current line to the list.
    }

    return Json(uploadModelRecords); //Return the list instead of a single instance.
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried with the var uploadModelRecords = new List<FileUploadViewModel>(); as you gave, this showing me one same rows of data for multiple rows, i meant same row duplicating till the eachValue length
Actually its working now I did not one line you mentioned thanks @diana

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.