Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6566829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:15:46+00:00 2026-05-25T14:15:46+00:00

I have an MVC3 site that I’ve setup for testing another site – most

  • 0

I have an MVC3 site that I’ve setup for testing another site – most of it has been quick and dirty, and so I’ve not gone to town creating model and view model types for all the views – only where input is requried from the user.

Okay so I have a controller method that projects a Linq sequence and sets it into ViewBag.

ViewBag.SomeData = Enumerable.Range(1,10).Select(i=> new { Value = i });

In my view (Razor C#) I then want to read this – quite simple:

@foreach(dynamic item in ViewBag.SomeData)
{
  @:Number: @item.i
}

Except, of course, I get a RuntimeBinderException because the anonymous type created in the controller is internal to the web project’s output assembly and the actual Razor code here will be running in a different assembly generated by the build manager so, all in all DENIED!

Obviously a ‘proper’ model type would solve the issue – but let’s say I simply don’t want to do that because that’s my prerogative(!) – how best to keep the code to a minimum and retain the dynamic-ness here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:15:47+00:00Added an answer on May 25, 2026 at 2:15 pm

    This is what I’ve done (and I stress this only really addresses the issue adequately for anonymous types); lifting the members out and pushing them into an ExpandoObject.

    My initial change was to make the projection a multi-statement that returns an ExpandoObject:

    ViewBag.SomeData = Enumerable.Range(1,10).Select(i=> {
      var toReturn = new ExpandoObject();
      toReturn.Value = i;
      return toReturn;
    });
    

    Which is almost as short as the anonymous type just not as clean.

    But then I wondered if I could grab the publicly readable members out of the anonymous type (the type is internal, but the properties it generates are public):

    public static class SO7429957
    {
      public static dynamic ToSafeDynamic(this object obj)
      {
        //would be nice to restrict to anonymous types - but alas no.
        IDictionary<string, object> toReturn = new ExpandoObject();
      
        foreach (var prop in obj.GetType().GetProperties(
          BindingFlags.Public | BindingFlags.Instance)
          .Where(p => p.CanRead)) // watch out for types with indexers 
        {
          toReturn[prop.Name] = prop.GetValue(obj, null);
        }
    
        return toReturn;
      }
    }
    

    Which means I can then use my original code – but with a little extension method call tagged on the end:

    ViewBag.SomeData=Enumerable.Range(1,10).Select(i=> new { Value = i }.ToSafeDynamic());
    

    It’s not efficient, and it should definitely not be used for serious production code. But it’s a good time saver.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework
I'm playing around with using ASP.NET MVC3 and have a very simple site that
I have an MVC3 site that needs to handle user posts. I am trying
I have a solution that has an MVC3 web front end. I have a
I have an asp.net mvc3 site that is showing some odd behavior when clicking
I have an MVC3 (razor) site published to IIS7 locally for testing purposes. When
I have published an ASP.NET MVC3 site. It runs great. However, looking back at
I use asp.net mvc3 and I want to write route that has no end..
I have a MVC3 site in C#, I have a particular view being fed
This is weird. I have a virtual directory setup for an MVC3 application called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.