I'm creating a List member variable during my Page_Init event. I'm having a problem referencing the objects in the list from my embedded C# code in the *.aspx page. The error is a Runtime Binder Exception that says "'object' does not contain a definition for 'JobID'".
When the debugger is invoked, I can see that the foreach loop's variable j does indeed have a dynamic property named JobID and it's filled with an int value. So, my question is why my embedded C# code can't work with the dynamic object. Is there an <%@ Import %> statement that I need to work with dynamic objects? I tried adding <%@ Import namespace="System.Dynamic" %> but that didn't help.
Thanks for the help. Mark
Code Behind:
using System;
using System.Collections.Generic;
using System.Linq;
using Jobbarama.WebCode;
using DataModel;
public partial class contact : System.Web.UI.Page
{
public List<dynamic> JobList { get; set; }
protected void Page_Init(object sender, EventArgs e)
{
SessionManager mgr = SessionManager.Current;
using (myEntities context = new myEntities())
{
var qry = from c in context.vjobList
where c.CampaignID == mgr.CampaignID
select new
{
c.JobID, c.JobTitle, c.CompanyName, c.InterestDate, c.InterestLevel
};
JobList = qry.ToList<dynamic>();
}
}
}
}
ASPX Code:
<select id='cboJob' name='cboJob' style='width: 150px;'>
<%foreach (var j in JobList){ %>
<option value="<%=j.JobID %>"><%=j.JobTitle%> [<%=j.CompanyName%>]</option>
<%} %>
</select>