我有一个主/子关系模型,我通过数据库优先导入生成。当我尝试在同一页面上输出子数据时,我会为每个子行而不是数据显示“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>
答案 0 :(得分:2)
由于它不知道如何在hashset中呈现项目,因此DisplayFor正在调用集合中每个项目的ToString方法。
答案 1 :(得分:0)
我遇到了同样的问题,但不知怎的,我的 .cshtml 文件(在 DisplayTemplates 中)未再发布了。
确保构建操作设置为内容。
花了我几个小时才弄明白,所以我想提一下它作为解决方案。
答案 2 :(得分:0)
我自己就碰到了这个。确保您拥有以下内容:
因此,作为使用测试内容的示例,您将执行与...非常相似的操作。
<强> /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。