Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
@{ ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

Index


@foreach (var Employee in ViewBag.name)// Error in this line
{

@Employee.EmpID


@Employee.Empname


};
Posted
Comments
Zoltán Zörgő 22-Mar-15 15:19pm    
1) Format your post!!!
2) I doubt that ViewBag.name is an IEnumerable...
[no name] 22-Mar-15 15:20pm    
He *did* format it ;-D
harshavardhan12345678 22-Mar-15 15:29pm    
my action metho

public ActionResult Index()
{
List<employee> Samples = new List<employee>()
{
new Employee{EmpID=1,Empname="Bhagyaraj"},
new Employee{EmpID=2,Empname="Mohan"}
};
return View();
}
Zoltán Zörgő 22-Mar-15 16:21pm    
And where do you pass Samples to the View?
Like return View(Samples);
harshavardhan12345678 22-Mar-15 15:26pm    
error is Object reference not set to an instance of an object.

1 solution

It is obvious that you din't set name property of ViewBag never...So it is null and for that can not be enumerated by foreach...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900