It's a ASP.NET Core MVC. I want to upload an excel file so I can view it in a List View, but I am stuck there. User is supposed to upload the file and then populate it in the list.
I previously wrote it on MVC, and I'm rewriting it in ASP.NET MVC Core. It's different since some code that I wrote for the MVC5 didn't work for Core, and I am stuck.
What I have tried:
Part of my List view:
<form asp-controller="Staff" asp-action="Upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<br/>
<button type="Submit">Upload</button>
<h1>Status: @ViewData["message"]</h1>
</form>
And this is my upload code.
[HttpPost]
public ActionResult Upload(IFormFile file, [FromServices] IHostingEnvironment oHE)
{
string fileName = $"{oHE.WebRootPath}\\UploadedFiles\\{file.FileName}";
foreach(string row in csvData.Split('\n'))
{
if(!string.IsNullOrEmpty(row))
{
buses.Add(new Bus
{
model = row.Split(',')[0],
bodyWork = row.Split(',')[1],
emissionStandard = Convert.ToInt32(row.Split(',')[2]),
year = Convert.ToInt32(row.Split(',')[3])
});
}
}
return View(new List<Staff>());
}
Help is appreciated.