rahul patil

rahul patil

  • NA
  • 160
  • 8.4k

how to display a list data from controller to view?

Jun 7 2020 6:04 AM
I want to display a list data form controller to view 
 
I m getting error:
    
 
student.cs
 
  1. public class Employee  
  2.  {  
  3.      public int Id { getset; }  
  4.   
  5.      public string Name { getset; }  
  6.   
  7.      public string Designaton { getset; }  
  8.   
  9.  }  
HomeController.cs
  1. public ActionResult Index()  
  2.  {  
  3.      List<Employee> emplist = new List<Employee>() {  
  4.              new Employee(){ Id=1, Name="Steve", Designaton = "hr" },  
  5.              new Employee(){ Id=2, Name="Bill", Designaton = "finance" },  
  6.              new Employee(){ Id=3, Name="Ram", Designaton = "hr" },  
  7.              new Employee(){ Id=4, Name="Ron", Designaton = "finance" },  
  8.              new Employee(){ Id=5, Name="Rob", Designaton = "finance" }  
  9.          };  
  10.   
  11.      ViewBag.totalemp = emplist.Count();  
  12.   
  13.      return View();  
  14.  }  
 Index.cshtml
 
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Index</h2>  
  5. <td>@((ViewBag.totalemp as IEnumerable<migrationdemo.Models.Employee>))</td>  
  6. @foreach (var item in ViewBag.totalemp)  
  7. {  
  8.     @item.Id  
  9.     @item.Name  
  10.     @item.Designaton  
  11. }  
 I want to displaying all data but now i am facing error?
 
 
 

Answers (3)