View is displaying "System.Data.Entity.DynamicProxies..." instead of actual Child data

8.1k Views Asked by At

I have a master/child relationship model that I generated via a database-first import. When I try and output the child data on the same page, I get "System.Data.Entity.DynamicProxies..." strings displayed for each child row instead of the data. I thought .NET MVC was smart enough to recognize the child collection and auto-output the row data (I would eventually add display/edit formatted templates). I am able to iterate through data via a loop in the main view, so the data is in the collection, but attempting to DisplayFor the child class is displaying proxy names or something instead. Anyone know what's up? Thanks.

The generated model (extra fields left out for brevity) is as follows:

namespace Test.Models
{

    public partial class HCCB_CR
    {
        public HCCB_CR()
        {
            this.HCCB_CR_CONTACTS = new HashSet<HCCB_CR_CONTACT>();
        }

        public string CCID { get; set; }
        public virtual ICollection<HCCB_CR_CONTACT> HCCB_CR_CONTACTS { get; set; }
    }
}

namespace Test.Models
{
    public partial class HCCB_CR_CONTACT
    {
        public decimal ID { get; set; }     //key
        public string CCID { get; set; }    // foreign key to hccb_cr

        public virtual HCCB_CR HCCB_CR { get; set; }
    }
}

Generated scaffolding for Details (abbreviated):

@model Test.Models.HCCB_CR
@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<fieldset>
    <legend>HCCB_CR</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.CCID)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CCID)
    </div>

    <div class="display-field">
        @Html.DisplayFor(model => model.HCCB_CR_CONTACTS)
    </div>
</fieldset>

Output:

CCID 
HCCB-CR100290 
System.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DASystem.Data.Entity.DynamicProxies.HCCB_CR_585E5CAF66461CDD0DF7F83DACDFCA848E78470297E638B38AB0FFDC8CBD83DA

What is System.Data.Entity.DynamicProxies.HCCB_CR_... and why am I getting?

UPDATE: Hoping this may help. Debug output when I step into DisplayFor:

The thread '<No Name>' (0x3e70) has exited with code 0 (0x0).
Step into: Stepping over non-user     code 'System.Web.Mvc.WebViewPage<Test2.Models.HCCBJEFF_CR>.Html.get'
The thread '<No Name>' (0x3e48) has exited with code 0 (0x0).
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_9D376C551350D68ED0D45F44495C7047639AE18E195454A7CE1DAB8EB66DDAF2.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR.CCID.get'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.CreateSourceQuery<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.ValidateLoad<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityCollection<Test2.Models.HCCBJEFF_CR_CONTACT>.Load'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR_CONTACT'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CONTACTID.set'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CNAME.set'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CCID.set'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CONTACTID.get'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.CheckKeyMatchesEntity'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CNAME.get'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.TakeSnapshot'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.CCID.get'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.TakeSnapshot'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR.get'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.GetBasePropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.EntityProxyFactory.TryGetBasePropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.EntityProxyFactory.CreateBaseGetter.AnonymousMethod__4'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.PocoPropertyAccessorStrategy.GetNavigationPropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.WillNotRefSteal'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal'
Step into: Stepping over non-user code 'Test2.Models.HCCBJEFF_CR_CONTACT.HCCBJEFF_CR.set'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_CONTACT_0D5EDED0A9ECB95525658A667586E43649A2EDCBFD933981026D54A30B929661.HCCBJEFF_CR.set'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.PocoPropertyAccessorStrategy.SetNavigationPropertyValue'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.Add'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityReference<Test2.Models.HCCBJEFF_CR>.ReferenceValue.set'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityReference.SetEntityKey'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupEntityReferenceToPrincipal'
Step into: Stepping over non-user code 'System.Data.Objects.EntityEntry.FixupReferencesByForeignKeys'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.FixupReferencesByForeignKeys'
Step into: Stepping over non-user code 'System.Data.Objects.ObjectStateManager.AddEntry'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Coordinator<Test2.Models.HCCBJEFF_CR_CONTACT>.ReadNextElement'
Step into: Stepping over non-user code 'System.Data.Common.Internal.Materialization.Shaper<Test2.Models.HCCBJEFF_CR_CONTACT>.SimpleEnumerator.MoveNext'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.Merge<Test2.Models.HCCBJEFF_CR_CONTACT>'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.EntityCollection<Test2.Models.HCCBJEFF_CR_CONTACT>.Load'
Step into: Stepping over non-user code 'System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad'
Step into: Stepping over non-user code 'System.Data.Objects.Internal.LazyLoadBehavior.LoadProperty<System.__Canon>'
Step into: Stepping over non-user code 'System.Data.Entity.DynamicProxies.HCCBJEFF_CR_9D376C551350D68ED0D45F44495C7047639AE18E195454A7CE1DAB8EB66DDAF2.HCCBJEFF_CR_CONTACTS.get'
Step into: Stepping over non-user code 'System.Web.Mvc.ModelMetadata.FromLambdaExpression<Test2.Models.HCCBJEFF_CR,System.Collections.Generic.ICollection<Test2.Models.HCCBJEFF_CR_CONTACT>>.AnonymousMethod__2'
Step into: Stepping over non-user code 'System.Web.Mvc.ModelMetadata.Model.get'
Step into: Stepping over non-user code 'System.Web.Mvc.Html.TemplateHelpers.TemplateHelper'
Step into: Stepping over non-user code 'System.Web.Mvc.Html.TemplateHelpers.TemplateFor<Test2.Models.HCCBJEFF_CR,System.Collections.Generic.ICollection<Test2.Models.HCCBJEFF_CR_CONTACT>>'

HCCB_CR_CONTACT template:

    @model Test.Models.HCCB_CR_CONTACT

    <fieldset>
        <legend>HCCB_CR_CONTACT</legend>

        <div class="display-label">
             @Html.DisplayNameFor(model => model.ID)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.ID)
        </div>
        <div class="display-label">
             @Html.DisplayNameFor(model => model.CCID)
        </div>
        <div class="display-field">
            @Html.DisplayFor(model => model.CCID)
        </div>
    </fieldset>
    <p>
        @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
    </p>
3

There are 3 best solutions below

0
On

I just ran into this myself. Make sure you have the following:

  1. In your Shared views folder, you need a folder title DisplayTemplates.
  2. In your DisplayTemplates folder you need your partial view, which is named exactly as your model (so in your case, the file would be /Shared/DisplayTemplates/HCCB_CR_CONTACT.cshtml, no underscore before the file name).

So as an example using your test stuff you would do something very similar to...

/Views/Shared/DisplayTemplates/HCCB_CR_CONTACT.cshtml

@model Test.Models.HCCB_CR_CONTACT

<fieldset>
    <legend>HCCB_CR_CONTACT</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.ID)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.ID)
    </div>
    <div class="display-label">
         @Html.DisplayNameFor(model => model.CCID)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.CCID)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

/Views/HCCB_CR_BlahBlah.cshtml

@model HCCB_CR

<div>
   <span>@Model.CCID</span>
   @Html.DisplayFor(model => model.HCCB_CR_CONTACTS)       
</div>  

Note: This method uses ASP.Net MVC 5 and .Net 4.5.1 with razor views.

12
On

Since it doesnt know how to render the items in the hashset, DisplayFor is calling the ToString method of each item in the set.

You may want to look at creating MVC display templates and edit templates.

0
On

I had the same problem, but somehow, my .cshtml file (in DisplayTemplates) was not published (anymore).

Make sure it's Build Action is set to Content.

Took me hours to figure out, so I would like to mention it as a solution.