1

我有一个通过数据库优先导入生成的主/子关系模型。当我尝试在同一页面上输出子数据时,我会为每个子行而不是数据显示“System.Data.Entity.DynamicProxies ...”字符串。我认为 .NET MVC 足够聪明,可以识别子集合并自动输出行数据(我最终会添加显示/编辑格式化模板)。我可以通过主视图中的循环遍历数据,因此数据在集合中,但尝试 DisplayFor 子类正在显示代理名称或其他内容。有谁知道怎么回事?谢谢。

生成的模型(为简洁起见省略了额外的字段)如下:

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; }
    }
}

为详细信息生成的脚手架(缩写):

@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>

输出:

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

什么是 System.Data.Entity.DynamicProxies.HCCB_CR_... 为什么我会得到?

更新:希望这可能会有所帮助。当我进入 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 模板:

    @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>
4

3 回答 3

2

由于它不知道如何呈现哈希集中的项目,因此 DisplayFor 正在调用集合中每个项目的 ToString 方法。

您可能希望查看创建 MVC 显示模板和编辑模板

于 2013-04-02T17:26:23.877 回答
0

我有同样的问题,但不知何故,我的.cshtml文件(在DisplayTemplates中)没有发布(不再)。

确保它的Build Action设置为Content

我花了几个小时才弄清楚,所以我想把它作为一个解决方案。

于 2015-03-09T16:08:27.963 回答
0

我自己也遇到了这个。确保您具备以下条件:

  1. 在您的共享视图文件夹中,您需要一个文件夹标题 DisplayTemplates。
  2. 在您的 DisplayTemplates 文件夹中,您需要部分视图,该视图的名称与您的模型完全相同(因此,在您的情况下,该文件将是 /Shared/DisplayTemplates/HCCB_CR_CONTACT.cshtml,文件名前没有下划线)。

因此,作为使用您的测试内容的示例,您将做一些非常类似于...

/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>  

注意:此方法使用带有剃刀视图的 ASP.Net MVC 5 和 .Net 4.5.1。

于 2015-07-20T15:53:04.907 回答