This project is read-only.

dynamic collection

Mar 9, 2011 at 5:27 AM

I am trying to parse a template passing in a model with dynamic collection, I keep getting exception  object' does not contain a definition for 'Number'

var cards = new List<dynamic>();
cards.Add(new { Number = "xxx", Balance = "00.0" });              

var model = new{Cards = cards};               

string message = Razor.Parse(template,model);         

@foreach (var item in Model.Cards)

{    <tr>        

<td> @item.Number   </td>      

<td> @String.Format("{0:c}", item.Balance) </td>    



Mar 9, 2011 at 9:12 AM


This is currently unsupported, we need to work on our dynamic object handling to get this to work. I would question the need for a static list of dynamic objects though, it would be safer and more efficient to declare a Card class.

Mar 9, 2011 at 8:25 PM
Edited Mar 9, 2011 at 8:49 PM

The problem stems from .net in that any dynamic is translated to object directly. so your List<dynamic> results in List<object> once it gets to our system. I'm not sure exactly how we could solve this problem in any way that doesn't require explicit information from the user on whether or not the model contains any dynamic objects as we won't have that information. This is an interesting problem and I have a possible solution - I'm not sure how well it will work yet.


So far it doesn't seem to work well. Though it's just a matter of me not fully understanding how ExpandoObject works. But I'm pretty sure I'm on the right track :)

Mar 9, 2011 at 9:39 PM

I am now passing in entities from EF directly and its working like dream. Thanks for the great work.