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 7033149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:01:39+00:00 2026-05-28T01:01:39+00:00

Possible Duplicate: Dynamic Anonymous type in Razor causes RuntimeBinderException I am trying to use

  • 0

Possible Duplicate:
Dynamic Anonymous type in Razor causes RuntimeBinderException

I am trying to use a dynamic type model in my MVC application. I have the following code:
in controller:

var model = new { Name = "test name", Family = "m" };
return this.View(model);

and in the view I have:

@model dynamic

@if(Model!=null)
{
   <p> @Html.Raw(Model.Name) </p>
}

When I am running this, I am getting the following error :

'object' does not contain a definition for 'Name'   (System.Exception {Microsoft.CSharp.RuntimeBinder.RuntimeBinderException)

Why do I get this error?
During debug, if I put my cursor on @Model, I can see that it has two property called Name and Family.

  • 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-28T01:01:40+00:00Added an answer on May 28, 2026 at 1:01 am

    What you have shown is not a dynamic type. It’s an anonymous type. There’s a huge difference.

    You cannot use anonymous types as models. The reason for this is because the compiler emits anonymous types as internal. This means that they are only accessible withing the current assembly. But as you know Razor views are compiled by the ASP.NET runtime as separate assemblies which have no way of using those anonymous types.

    Obviously the correct way in this case is to use a view model:

    public class MyViewModel
    {
        public string Name { get; set; }
        public string Family { get; set; }
    }
    

    and then have your controller action pass this view model to the view:

    var model = new MyViewModel { Name = "test name", Family = "m" };
    return this.View(model);
    

    so that your view can work with it:

    @model MyViewModel
    @if (Model != null)
    {
        <p>@Model.Name</p>
    }
    

    Some people (not me, I would never recommend anything like this) also use ViewBag and this way they don’t need a model:

    ViewBag.Name = "test name";
    ViewBag.Family = "m";
    return this.View();
    

    and then in the view:

    <p>@ViewBag.Name</p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I pass an anonymous type to a method? Im trying
Possible Duplicate: Django dynamic model fields Good Morning guys! Scenario is the following. For
Possible Duplicate: How will you use the C# 4 dynamic type ? What would
Possible Duplicate: Why use dynamic typing in c#? Should the new C# type 'dynamic'
Possible Duplicate: What’s with the love of dynamic Languages I have already read this
Possible Duplicate: How do you generate dynamic (parameterized) unit tests in Python? I have
Possible Duplicate: Dynamic array keys I have an array $Values which is set up
Possible Duplicate: MySQL dynamic cross tab I have student_record table ------------------------------------------------------------------- student_id | semester
Possible Duplicate: Auto-size dynamic text to fill fixed size container. Let's say i have
Possible Duplicate: Javascript Objects: Dynamic variable names? I have a json string passed in

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.