我使用實體框架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'。 –