0

I was trying to bind View Bag data to a HTML table. My C# code works fine. Can add data to View Bag as ViewBag.employeeData = getDBData(); but when i try to access each item,

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException is thrown.

Below given my C# code

   private IEnumerable<Employees> getDBData()
    {
        SqlConnection con = null;
        Employees empData;
        string sqlConn = ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString;
        try
        {
            using (con = new SqlConnection(sqlConn))
            {
                SqlCommand command = new SqlCommand("SELECT ID,FirstName,LastName,Gender,Salary FROM Employees", con);
                con.Open();
                SqlDataReader read = command.ExecuteReader();
                List<Employees> empDetails = new List<Employees>();
                while (read.Read())
                {
                    empData = new Employees();
                    empData.ID = Convert.ToInt32(read["ID"]);
                    empData.FirstName = Convert.ToString(read["FirstName"]);
                    empData.LastName = Convert.ToString(read["LastName"]);
                    empData.Gender = Convert.ToString(read["Gender"]);
                    empData.Salary = Convert.ToInt32(read["Salary"]);
                    empDetails.Add(empData);
                }
                return empDetails;
            }
        }
        catch (Exception)
        { return null; }
        finally { con.Dispose(); }
    }

Razor

        <table class="table table-striped table-condensed">
        <thead>
            <tr>
                <th>ID</th>
                <th>FirstName</th>
                <th>LastName</th>
                <th>Gender</th>
                <th>Salary</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in ViewBag.employeeData)
            {
                <tr>
                    <td>@item.ID</td>
                    <td>@item.FirstName</td>
                    <td>@item.LastName</td>
                    <td>@item.Gender</td>
                    <td>@item.Salary</td>
                </tr>
            }
        </tbody>
    </table>

Exception:

'object' does not contain a definition for 'ID'.

How to resolve this?

James Z
  • 12,209
  • 10
  • 24
  • 44
Learn Avid
  • 59
  • 2
  • 13
  • You should dispose of your `SqlCommand` and your `SqlReader`. – Erik Philips Nov 03 '17 at 23:38
  • Sure, Erik Philips. But i think writing `SqlCommand` & `SqlReader` inside a `using{ }` statement will be disposed automatically. Correct me, if i'm wrong – Learn Avid Nov 05 '17 at 02:43
  • Yes, you are incorrect. The only thing disposed is the Connection. [*The using statement calls the Dispose method on the object in the correct way, and (when you use it as shown earlier) it also causes the object itself to go out of scope as soon as Dispose is called.*](https://stackoverflow.com/questions/75401/uses-of-using-in-c-sharp). **Only** the objects specified in the using statement are disposed. Anything else in the code block are not. – Erik Philips Nov 06 '17 at 17:45
  • Thanks, **Erik Philips** for your clear response. – Learn Avid Nov 09 '17 at 16:39

2 Answers2

0

You don't need to use the ViewBag (and I would recommend never using it). When you return a value from a controller:

return empDetails;

It becomes the model for the view. In the view, you need to declare the model type:

@model List<Employees>

Then you can:

@foreach (var item in Model)
{
  <tr>
    <td>@item.ID</td>
    <td>@item.FirstName</td>
    <td>@item.LastName</td>
    <td>@item.Gender</td>
    <td>@item.Salary</td>
  </tr>
}

How does razor know to hold List which is returned from controller class.

This is the designed convention of . Dynamic v. Strongly Typed Views.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • It woked !! But i didn't create a strongly-typed view. How does razor know to hold `List` which is returned from controller class.? Correct me if my understanding is wrong. – Learn Avid Nov 05 '17 at 02:31
-1

It's quite hard to debug your problem as you didn't provide your data properly. But you can try this to get the data from @foreach -

@foreach (var item in Model.Foo.Foo1)
{
    @Html.DisplayFor(modelItem=> item.fooName)
}

and check for all the items you are iterating that maybe you miss out some values or maybe it was a spell mistake

piedpiper
  • 520
  • 5
  • 18