2

What I try is add some dummy records which I want to use in an ASP.NET MVC 3 view to provide data for some experiments. I try this:

var dummyData = new[]
            {
                new  {Row = 1, Col = 1, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
                new  {Row = 1, Col = 2, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
                new  {Row = 2, Col = 1, IsRequired = true, QuestionText = "No?", FieldValue = "string"},
                new  {Row = 3, Col = 1, IsRequired = false, QuestionText = "No?", FieldValue = "string"}
            }.ToList();
            ViewBag.Header = dummyData;

However when I try to use the data in my view :

@{
          foreach (var item in ViewBag.Header)
          {

              <tr><td>@item.QuestionText</td><td>@item.FieldValue</td></tr>

          }
       }

I get this error - 'object' does not contain a definition for 'QuestionText'. I'm thinking that there's something wrong with the way I create the list but not 100% sure.

Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116
Leron
  • 9,546
  • 35
  • 156
  • 257
  • 5
    I would advise you do not rely on anonymous objects and the view bag so much. Things are much better when they are strongly typed – musefan Apr 22 '13 at 15:26
  • Related... http://stackoverflow.com/questions/713521/accessing-c-sharp-anonymous-type-objects I would suggest using a Tuple class. – Jace Rhea Apr 22 '13 at 15:26
  • why don't you use viewmodel instead? – Idrees Khan Apr 22 '13 at 15:30
  • see my this example of creating viewmodel => http://stackoverflow.com/questions/15432246/creating-a-mvc-viewmodels-for-my-data/15436044#15436044 – Idrees Khan Apr 22 '13 at 15:32
  • I'm learning on the fly. Have to check what exactly `viewmodel` can do for me here, and also I have previous experience with Windows Forms application where we vastly use anonymous objects and it was perfectly fine so I just decided to follow this line. – Leron Apr 22 '13 at 15:33
  • @DotNetDreamer While that may be a better practice it really has nothing to do with the question being asked. – Jace Rhea Apr 22 '13 at 15:38
  • @JaceRhea, that's why i put it in comments. It was suggestion. If you like it go ahead use it otherwise i have no complain about it – Idrees Khan Apr 22 '13 at 15:40

3 Answers3

3

An anonymous type is local to the scope from which it was declared. You are not going to easily be able to get properties off it outside the scope of the type declaration. Related question.

I would suggest using a Tuple or just create a simple POCO object for the data.

var dummyData = new[]
        {
            Tuple.Create(1, 1, true, "Yes?", "int"),
        }.ToList();
        ViewBag.Header = dummyData;
Community
  • 1
  • 1
Jace Rhea
  • 4,982
  • 4
  • 36
  • 55
2
var dummyData = new List<dynamic>
        {
            new  {Row = 1, Col = 1, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
            new  {Row = 1, Col = 2, IsRequired = true, QuestionText = "Yes?", FieldValue = "int"},
            new  {Row = 2, Col = 1, IsRequired = true, QuestionText = "No?", FieldValue = "string"},
            new  {Row = 3, Col = 1, IsRequired = false, QuestionText = "No?", FieldValue = "string"}
        };
        ViewBag.Header = dummyData;

That should do the trick.

b_meyer
  • 594
  • 2
  • 7
1

Change your foreach definition to this:

@{ foreach (dynamic item in ViewBag.Header) {

the problem is that they are anonymous classes so they need to be used as dynamic classes so the CLR can late bind the object at runtime.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232