0

In Controller i have a viewbag which is defined as

var aut = from a in db.Model
            where a.UniqueReqID == vvid
            join b in db.Model2 on a.DeptID equals b.DepartmentID
            select new { a, b };
ViewBag.at = aut;

In View I want to access the fields extracted. like

foreach (var item in ViewBag.at)
{
    int i = 0;
    <tr>
        <td>
            <input type="text" name="DeptID" id="DeptID" value=@item.a.DepartmentName />
        </td>
        <td>
            <input type="text" name="CodeEntered" id="CodeEntered" @i placeholder="Enter Verification Code Received" />
        </td>
    </tr>
    i++;
}

At Runtime I am getting an error "'object' does not contain a definition for 'a'" How can I access all the variables inside viewbag

Markus Meyer
  • 3,327
  • 10
  • 22
  • 35

1 Answers1

0

The view bag container is basically a dictionary where the values are objects. Thus in the view you have to cast the value to an a concrete class. You are using anonymous so this make it difficult to do. See Storing an Anonymous Object in ViewBag.

If you really want to use the view bag I would recommend creating a class and storing that in the viewbag.

But I would strongly recommend using a model instead.

Frank Navarrete
  • 121
  • 2
  • 11