【发布时间】:2015-09-23 15:12:48
【问题描述】:
我尝试在 Render 操作中访问模型 (FilePath) 的属性,但没有成功。
我有这些模型:
public class Offer
{
public int OfferID { get; set; }
public string Reference { get; set; }
public string Title { get; set; }
public virtual ICollection<FilePath> FilePaths { get; set; }
}
public class FilePath
{
public int FilePathId { get; set; }
public string FileName { get; set; }
public virtual Offer Offer { get; set; }
}
在控制器报价中:
public PartialViewResult Category()
{
var offers = db.Offers
.Include(i => i.FilePaths)
// i tried with .Include(“FilePaths”) with no sucess
.ToList();
return PartialView("_OfferBoxList", offers);
}
显示为:
@{Html.RenderAction("Category", "Offer");}
问题出在部分动作视图中:
@foreach (var item in Model)
{
@item.Title // This work
@item.FilePath.FileName // This NOT work
}
输出错误:
“System.Data.Entity.DynamicProxies.Offer_278BF6A1F89FB9514329AC922E992AEBD19368D66A4460B6BEBA1BB2256CAFC3”不包含“FilePath”的定义
感谢您的帮助。
【问题讨论】:
-
如果你关闭延迟加载,我认为它会解析为类型,而不是代理。但是,你真的不应该将你的实体加载到视图中。我通常创建 DTO,而不是仅在查询中塑造数据,但也可用作 dataItems/dataSource 用于这些目的..
-
另外,
FilePaths(复数)是Offer的属性,它是Collection<FilePath>..@item.FilePath(单数)甚至不存在这个代码sn-p ..
标签: c# entity-framework lambda renderaction