1

Controller code

var list = (from c in db.table where c.SUPERVISOR_ID == EmployeeId select new  {ID=c.PERSON_ID,Name=c.PERSON_NAME }).ToList();

ViewBag.BrList = list;

return View();

Razor code:

   @foreach (var br in ViewBag.BrList)
   {
     <option value="@br.ID">@br.Name</option> //throws error 
  }

Above mentioned code gives error - 'object' does not contain a definition for 'ID'

s.k.paul
  • 7,099
  • 28
  • 93
  • 168
  • 1
    I'm not sure you can use an anonymous object in a ViewBag and use it this way. Try to project in a custom object, not in an anonymous one. – Raphaël Althaus Jan 25 '14 at 09:32
  • 2
    As Raphaël says the problem is anonymous object see this :http://stackoverflow.com/questions/5649536/stuffing-an-anonymous-type-in-viewbag-causing-model-binder-issues – Sameer Jan 25 '14 at 09:37
  • Another solution to the same problem: http://stackoverflow.com/questions/5120317/dynamic-anonymous-type-in-razor-causes-runtimebinderexception – Max Yakimets Jan 25 '14 at 09:38

0 Answers0