I have to display my details in the list view .I am storing my details in controller using viewdata and i need to use the viewdata in my view .The view part is not working.IN view part i have to use view data inside foreach and iterate through the list .Help me out!!
Previously i was storing in static list given below:
public static List<EmployeeModel> staticEmployeeViewModelList = new List<EmployeeModel>();
my controller part
public async Task<IActionResult> ImportEmployeeDetails(IFormFile excelfile)
{
try
{
EmployeesViewModelList employeesListObject = new EmployeesViewModelList();
// var employeesListObject = new EmployeesViewModelList();
List<EmployeeModel> employeesViewModelList = new List<EmployeeModel>();
if (excelfile == null || excelfile.Length == 0)
{
return View(employeesListObject);
}
var supportedTypes = new[] { ".xls", ".xlsx" };
var ext = Path.GetExtension(excelfile.FileName);
if (!supportedTypes.Contains(ext))
{
return View(employeesListObject);
}
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot",
"EmployeeDetails.xlsx");
FileInfo file = new FileInfo(path);
using (ExcelPackage package = new ExcelPackage(file))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns;
for (int i = 2; i <= rowCount; i++)
{
EmployeeModel emp = new EmployeeModel();
// emp.EmployeeId = Convert.ToInt32(worksheet.Cells[i, 1].Value.ToString());
emp.EmpEin = worksheet.Cells[i, 1].Value.ToString();
emp.EmpFirstName = worksheet.Cells[i, 2].Value.ToString();
employeesViewModelList.Add(emp);
}
ViewData["EmployeeList"] = employeesViewModelList;
//to get data
employeesViewModelList = ViewData["EmployeeList"] as List<EmployeeModel>;//convert back to llist
staticEmployeeViewModelList = employeesViewModelList.ToList();
ViewData["EmployeeList"] = employeesViewModelList.ToList();
employeesListObject.EmpModelList = employeesViewModelList;
employeesViewModelList = ViewData["EmployeeList"] as List<EmployeeModel>;
// return View(employeesListObject);
return View(employeesListObject);
}
}
catch(Exception ex)
{
ViewData["Message"] = "Opps! Something Went wrong!";
return RedirectToAction("ExcelPackage");
}
}
My View:
Мой взгляд
// в цикле foreach я использовал приведенный ниже код
@foreach (var employee in (List) TempData ["EmployeeList"])
{
@foreach (var item в employee.EmpModelList.ToList ())
{
// вышеприведенный не работает
-------------------------------------------------- ------------------------
Сохранить
EmpEin
Имя
Фамилия
Эл. адрес
Страна
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.EmpModelList.ToList())
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmpEin)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpFirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpLastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.HomeCountry)
</td>
<td>
<td><a class="edit" href="javascript:;">Edit</a></td>
<td class="EmpId" style="display:none">@Html.DisplayFor(modelItem => item.EmpEin)</td>
</td>
</tr>
}
</tbody>
</table>
</div>
}