6

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>
2
  • I'm having the same difficulty with embedded code where my code-behind returns an IEnumerable<dynamic>. It appears somewhere my dynamic is being converted to an object or something. @sisdog, even if you modify your foreach to look like <%foreach (dynamic j in JobList) %> it doesn't seem to work. Commented Feb 21, 2011 at 19:16
  • While not an answer to why it's not working, you could rewrite your code to use DataBinder.Eval which would resort to using reflection to grab the JobID property. Commented Feb 21, 2011 at 22:00

2 Answers 2

1

My guess this might be a permission issue due to using an anonymous class and aspx late compiling stuff in different assemblies.

You can use impromptu-interface to make this work.

using ImpromptuInterface

then you make an Interface (I'm using dynamic because i don't know your types)

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

Your Property should use the interface

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

And to create your list just add .AllActLike<ISelectJob>()

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

And this should work, as it generates a lightweight dlr proxy and sets the context to the anonymous class its self so it thinks it always has access, unlike calling with the dynamic keyword.

Sign up to request clarification or add additional context in comments.

4 Comments

Awesome but I couldn't get it to work. I get this error: LINQ to Entities does not recognize the method 'test.ISelectJob ActLike[ISelectJob](System.Object, System.Type[])' method, and this method cannot be translated into a store expression Make any sense to you?
Yes, my mistake i wasn't think IQueryable, i can fix the example.
Done. I think it reads a lot clearer too. Although you don't have to make JobList have the static type, if there was a reason it needed to be List<Dynamic> then the last line could be JobList = qry.AllActLike<ISelectJob>().ToList<dynamic>(); just the same.
interesting solution to what will be an increasingly frequent problem
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.