我使用实体框架5在我的项目中首先使用数据库。我相信在外键虚拟字段中延迟加载值时存在一些问题,并且我尝试了各种方法,但目前为止我无法解决此问题。延迟加载相关实体不与实体框架5.0工作
下面是详细信息:
我有一个CustomerDiscountCardController指数方法如下:
public ActionResult Index(String searchTerm = null, int page = 1)
{
var model = CustomerDiscountCardPagedList(searchTerm, page);
ViewBag.CustomerID = new SelectList(db.Customers.OrderBy(c => c.FirstName), "CustomerID", "FirstName");
if (Request.IsAjaxRequest())
{
return PartialView("_CustomerDiscountCard", model);
}
return View(model);
}
private IPagedList<CustomerDiscountCard> CustomerDiscountCardPagedList(String searchTerm, int page)
{
var model = db.CustomerDiscountCards.Include(d => d.DiscountCard).
OrderBy(c => c.DiscountCard.CardName).
Where(c => (c.DiscountCard.CardName.Contains(searchTerm))).
Select(c => new
{
DiscountCardID = c.DiscountCardID,
CustomerID = c.CustomerID,
IssueDate = c.IssueDate,
ExpiryDate = c.ExpiryDate,
CustomerDiscountCardID = c.CustomerDiscountCardID
}
).ToList().Select(c => new CustomerDiscountCard
{
DiscountCardID = c.DiscountCardID,
CustomerID = c.CustomerID,
IssueDate = c.IssueDate,
ExpiryDate = c.ExpiryDate,
CustomerDiscountCardID = c.CustomerDiscountCardID
}).ToPagedList(page, 4);
return model;
}
我的数据上下文类看起来如下:
public OnlineFoodOrderingEntities2()
: base("name=OnlineFoodOrderingEntities2")
{
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
}
//Entities class code....
}
我的自动生成的CustomerDiscountCard类看起来如下所示:
public partial class CustomerDiscountCard
{
public int CustomerDiscountCardID { get; set; }
public Nullable<int> DiscountCardID { get; set; }
public Nullable<int> CustomerID { get; set; }
public Nullable<System.DateTime> IssueDate { get; set; }
public Nullable<System.DateTime> ExpiryDate { get; set; }
public virtual Customer Customer { get; set; }
public virtual DiscountCard DiscountCard { get; set; }
}
现在的问题是当我的索引方法被调用Ajax请求返回_CustomerDiscountCard
布局的代码的局部布局是如下:
@using PagedList;
@using PagedList.Mvc;
@using OnlineFoodOrderingMVC.Models;
@model IPagedList<CustomerDiscountCard>
<div id="targetCustomerDiscountCardList">
<div class="pagedList" data-ofo-target="#targetCustomerDiscountCardList">
@Html.PagedListPager(Model, page => Url.Action("Index", "CustomerDiscountCard", new { page }),
PagedListRenderOptions.MinimalWithItemCountText)
</div>
<div style="clear:both"></div>
<div>
<table class="entity_record_table">
<tr class="entity_record_table_header">
<th class="entity_record_table_header_item_text">
CustomerDiscountCards
</th>
<th class="entity_record_table_header_link_text">
Edit
</th>
<th class="entity_record_table_header_link_text">
View
</th>
<th class="entity_record_table_header_link_text">
Delete
</th>
</tr>
@{
String[] rowDisplay = { "entity_record_row_display1", "entity_record_row_display2" };
String currentClass = "entity_record_row_display1";
int count = 0;
}
@foreach (var item in Model)
{
count++;
currentClass = rowDisplay[count % 2];
<tr class="@(currentClass)">
<td class="entity_record_table_item">
@Html.DisplayFor(modelItem => item.DiscountCard.CardName)
</td>
<td class="entity_record_table_link">
@Ajax.ActionLink(" ", "Edit", new { id = item.CustomerDiscountCardID },
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "targetDiv"
}
, new { @class = "entity_record_edit" })
</td>
<td class="entity_record_table_link">
@Ajax.ActionLink(" ", "Details", new { id = item.CustomerDiscountCardID },
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "targetCustomerDiscountCardList"
}
, new { @class = "entity_record_view" })
</td>
<td class="entity_record_table_link">
@Ajax.ActionLink(" ", "Delete", new { id = item.CustomerDiscountCardID },
new AjaxOptions
{
HttpMethod = "get",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "targetCustomerDiscountCardList"
}
, new { @class = "entity_record_delete" })
</td>
</tr>
}
</table>
</div>
</div>
所以当剃刀发动机渲染此局部视图矿山和执行下列指令
item.DiscountCard.CardName
我得到DicountCard为空,我不能够明白是什么原因造成这是空任何一个可以请帮我 这个。 非常感谢。
对于一个在你的剃须刀@ Html.DisplayFor你正在做的'@ Html.DisplayFor(modelItem => item.DiscountCard。CardName)'请注意,您将模型作为** modelItem **进行调用,但是您将**项目**将**项目**更改为** modelItem ** – dklingman
我刚刚添加了代码的快照,以便查看器将理解我想要实现的,如果你想要发布完整的剃刀视图代码,如下所示 – user3649270
@dklingman:OP在“foreach”块中执行此操作。正确的表达式*是*'modelItem => item'。 –