10

I have a list of Contact objects, from which, I just want a subset of attributes. So I used LINQ projection to create an anonymous list and I passed that to a partial view. But when I use that list in partial view, compiler says that it doesn't have those attributes. I tried the simplest case as follow, but still I have no chance to use an anonymous object or list in a partial view.

var model = new { FirstName = "Saeed", LastName = "Neamati" };
return PartialView(model);

And inside partial view, I have:

<h1>Your name is @Model.FirstName @Model.LastName<h1>

But it says that @Model doesn't have FirstName and LastName properties. What's wrong here? When I use @Model, this string would render in browser:

 { Title = "Saeed" }
3
  • You've run into the often bemoaned and discussed subject of anonymous types having their properties declared internal, which leads to your scenario. A solution is offered at this link that does work. stackoverflow.com/questions/5120317/…. Preferably however, you would have a concrete type to use for your view model, but this is an option. Commented Jul 2, 2011 at 19:33
  • 1
    Same question pretty much here. Same answer as Khepri stackoverflow.com/questions/4862122/… Commented Jul 2, 2011 at 21:35
  • use @model dynamic Commented Jun 27, 2015 at 12:05

2 Answers 2

17

Don't do this. Don't pass anonymous objects to your views. Their properties are internal and not visible in other assemblies. Views are dynamically compiled into separate dynamic assemblies by the ASP.NET runtime. So define view models and strongly type your views. Like this:

public class PersonViewModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

and then:

var model = new PersonViewModel 
{ 
    FirstName = "Saeed", 
    LastName = "Neamati" 
};
return PartialView(model);

and in your view:

@model PersonViewModel
<h1>Your name is @Model.FirstName @Model.LastName<h1>
Sign up to request clarification or add additional context in comments.

3 Comments

Is this still the case in latest versions of MVC - i.e. that anonymous objects are internal and not visible?
what about @model dynamic?
@Darin does every view (cshtml) have their own assembly?
1

Use Reflection to get the values, preformance a bit slower, but no need ot create unnesasry models

Add next class to your application

public class ReflectionTools
{
    public static object GetValue(object o, string propName)
    {
        return o.GetType().GetProperty(propName).GetValue(o, null);
    }
}

in your view use next code

            @(WebUI.Tools.ReflectionTools.GetValue(Model, "Count"))

Hope that helps

2 Comments

Using reflection to solve this problem is a horrible solution. It is costly and unnecessary and will lead to more problems down the road.
That is 100% working solution, it answers how to work with anonymous objects, i would like to hear from you how to work with that in a correct way?

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.