Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't Reference Dynamic Objects in Embedded ASPX code
    text
    copied!<p>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'".</p> <p>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 &lt;%@ Import %> statement that I need to work with dynamic objects? I tried adding &lt;%@ Import namespace="System.Dynamic" %> but that didn't help.</p> <p>Thanks for the help. Mark</p> <p><strong>Code Behind:</strong></p> <pre><code> using System; using System.Collections.Generic; using System.Linq; using Jobbarama.WebCode; using DataModel; public partial class contact : System.Web.UI.Page { public List&lt;dynamic&gt; 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&lt;dynamic&gt;(); } } } } </code></pre> <p><strong>ASPX Code:</strong></p> <pre><code>&lt;select id='cboJob' name='cboJob' style='width: 150px;'&gt; &lt;%foreach (var j in JobList){ %&gt; &lt;option value="&lt;%=j.JobID %&gt;"&gt;&lt;%=j.JobTitle%&gt; [&lt;%=j.CompanyName%&gt;]&lt;/option&gt; &lt;%} %&gt; &lt;/select&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload