Search code examples
linqasp.net-mvc-4razor-2

'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' 'object' does not contain a definition for 'intEmployeeId'


I am getting an exception when i trying to display the values of viewbag. I have joined two tables and stored that data in viewbag using Linq.

This is my controller action method

    public ActionResult HRSeparationDetails()
    {
        //List<trnEsSeparationDetail> separationlist = (from list in db.trnEsSeparationDetails
        //                                              select list).ToList();


        ViewBag.SeparationList = (from list in db.trnEsSeparationDetails
                                                      join R in db.mstEsEmpReasonForSeparations
                                                          on list.ReasonForSeperationId equals R.intSeperationId
                                                      select new
                                                      {
                                                          intEmployeeId = list.intEmpId,
                                                          ReasonForSeparation = R.txtReasonForSeperation,
                                                          ResiganationDate = list.dtResignationDate,
                                                          RelievingDate = list.dtRequestedRelievingDate,
                                                          SeparationRemarks = list.txtRemark
                                                      }).ToList();

         return View();
    }

This is my viewpage

     @model IEnumerable<EMPApp.Models.trnEsSeparationDetail>
     @{
     ViewBag.Title = "HRSeparationDetails";
        Layout = "~/Views/Shared/_Layout.cshtml";
     }

      <h2>HRSeparationDetails</h2>
      <div>
     @if (ViewBag.SeparationList == null)
    {
        <div style="color:green"><h3>@ViewBag.SeparationerrorMessage</h3></div>
    }
    else
    { 
        <table class="grid" cellpadding="0" cellspacing="0">
            <tr>
                <th>
                    <b>Employee Id</b>
                </th>
                @*<th>
                    <b>ReasonForSeparationId</b>
                </th>*@
                <th>
                    <b>Reason For Separation</b>
                </th>
                <th>
                    <b>Resignation Date</b>
                </th>
                <th>
                    <b>Relieving Date</b>
                </th>
                <th>
                    <b>Separation Remarks</b>
                </th>
            </tr>
            @foreach (var item in ViewBag.SeparationList)
            {
                <tr>
                    <td>
                        @item.intEmployeeId
                        @*@{
                            Session["SeparationEmpId"] = item.intEmpId;
                        }
                        @Html.ActionLink(Session["SeparationEmpId"].ToString(), "EmployeeDashboard", "HRAdmin", new { id = @item.intEmpId }, null)*@
                    </td>
                    @*<td>
                        @item.ReasonForSeperationId
                    </td>*@
                    <td>
                        @item.ReasonForSeparation
                    </td>
                    <td>
                        @item.ResiganationDate
                    </td>
                    <td>
                        @item.RelievingDate
                    </td>
                    <td>
                        @item.SeparationRemarks
                    </td>
                </tr>
            }
        </table>
    }

Any help will be appreciated. Thanks..


Solution

  • ViewBag is a dynamic object and your setting the property to a collection of anonymous objects. In you foreach loop

    @foreach (var item in ViewBag.SeparationList)
    

    item is typeof object and object does not have a property intEmployeeId (or any of the other properties your define in the anonymous object). One way to handle this is to create a view model with the properties you want to display

    public class SeparationDetailVM
    {
      public int intEmployeeId { get; set; }
      public DateTime ResiganationDate { get; set; }
      ....
    }
    

    and in the controller

    var details = (from list in ....
                   select new SeparationDetailVM
                   {
                      intEmployeeId = list.intEmpId,
                      ResiganationDate = list.dtResignationDate,
                      ....
                   }).AsEnumerable();
    return View(details); // no need for ViewBag
    

    and in the view

    @model IEnumerable<YourAssembly.SeparationDetailVM>
    ....
    @foreach (var item in Model)
    {
      ....
      <td>@item.intEmployeeId</td>
      <td>@item.ResiganationDate</td>
      ....
    }