Unable to reference dynamic objects in embedded aspx code

I create a List member variable during my Page_Init event. I had a problem with a link to the objects in the list from my embedded C # code on the * .aspx page. The error is a Buntime Binder Exception, which says that the "object" does not contain a definition for the "JobID".

When the debugger is called, I see that the foreach loop variable j really has a dynamic property called JobID and is populated with an int value. So my question is why my C # inline code cannot work with a dynamic object. Is there a <% @ Import%> statement that should work with dynamic objects? I tried adding <% @Import namespace = "System.Dynamic"%>, but that didn't help.

Thanks for the help. Mark

Code for:

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> 
+6
source share
2 answers

I suppose this could be a permission issue due to using an anonymous class and late compilation in aspx in different assemblies.

You can use impromptu-interface to make this work.

 using ImpromptuInterface 

then you create an interface (I use dynamic because I don't know your types)

 interface ISelectJob dynamic JobID dynamic JobTitle dynamic CompanyName dynamic InterestDate dynamic InterestLevel } 

Your property should use an interface

 public List<ISelectJob> JobList { get; set; } 

And to create a list, simply add .AllActLike<ISelectJob>()

 JobList = qry.AllActLike<ISelectJob>().ToList(); 

And this should work, as it generates a lightweight dlr proxy and sets the context for the anonymous class, so it believes that it always has access, unlike a call with the dynamic keyword.

+1
source share

What about using LinqDataSource, setting the OnSelecting command, and using a relay or datalist to display?

+1
source share

All Articles