我在我的项目中首先使用数据库实体框架5。我相信在外键虚拟字段中延迟加载值存在一些问题,我已经尝试了所有可能的方法,但到目前为止我无法解决问题。
以下是详细信息:
我有一个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为null,我无法理解是什么导致这个为null可以任何人请帮助我这个。 非常感谢。
答案 0 :(得分:1)
问题是DiscountCard
确实 为空。在您提取项目时,您使用Select
从值的子集中创建新类:
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
})
首先,你所做的是重复的。没有理由选择匿名对象,强制转换为列表,然后选择具有相同数据的真实类实例。只需直接选择类实例,即:
Select(c => new CustomerDiscountCard
{
DiscountCardID = c.DiscountCardID,
CustomerID = c.CustomerID,
IssueDate = c.IssueDate,
ExpiryDate = c.ExpiryDate,
CustomerDiscountCardID = c.CustomerDiscountCardID
})
其次,您从未为DiscountCard
设置值,仅为DiscountCardId
设置值。当您创建这样的新实例时,它不附加到您的上下文,并且无法获取或检索此相关值。这带给我们......
第三,你为什么一开始就选择任何东西?您从可枚举的CustomerDiscountCard
开始,然后选择可枚举的新CustomerDiscountCard
实例。这样做的唯一方法是打破这些项目对你的上下文的附件,有效地杀死任何延迟加载任何东西的能力。
<强>更新强>
只需将其更改为:
var model = db.CustomerDiscountCards.Include( d => d.DiscountCard).
OrderBy(c => c.DiscountCard.CardName).
Where(c => (c.DiscountCard.CardName.Contains(searchTerm))).
ToPagedList(page, 4);