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.

7
  • 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 Commented Apr 22, 2013 at 15:26
  • Related... stackoverflow.com/questions/713521/… I would suggest using a Tuple class. Commented Apr 22, 2013 at 15:26
  • why don't you use viewmodel instead? Commented Apr 22, 2013 at 15:30
  • see my this example of creating viewmodel => stackoverflow.com/questions/15432246/… Commented Apr 22, 2013 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. Commented Apr 22, 2013 at 15:33

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Not what I really wanted but closest to some solution.
3
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.

Comments

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.

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.