ASP.NET MVC 3 アプリケーション (C#) を実行しています。
最近、データベースの外部キーをヌル可能にしました。これはおそらくベスト プラクティスではありませんが、私のシナリオでは必要です。したがって、レポート (ASPX ビュー エンジン) を表示すると、データベースで null 可能にしたオブジェクトの値が表示されません。
現在のデータ構造 (単純化) は次のとおりです。
Inventory
________________
InventoryID int PK
ShopID FK int NOT NULL
ProductID FK int NULL
これに対する唯一の変更点は、ProductID が Nullable ではないことです。
私の ActionResult は次のとおりです。
[HttpPost]
public ActionResult InventoryReport(FormCollection formVariables, InventoryReportRequest reportRequest)
{
ModelContainer ctn = new ModelContainer();
var query = ctn.Inventory.Where(ic => !ic.Deleted && ic.ShopID == reportRequest.ShopID);
if (reportRequest.ProductID.HasValue)
{
query = (from ic in query
where reportRequest.ProductID == ic.ProductID
select ic);
}
List<Inventory> checks = query.ToList();
if (checks.Count > 0)
{
return View(checks);
}
else
{
return RedirectToAction("Index");
}
}
このアクションでデバッグすると、ProductID が取得されていることがわかりますが、Product オブジェクトは null のままです。したがって、ビューに結果を表示したいときは -
<table class="normal" width="100%">
<tr>
<th>
Date
</th>
<th>
Shop
</th>
<th>
**Product Name**
</th>
<th>
**Product ID**
</th>
<th>
Active
</th>
</tr>
<% foreach (var item in Model) { %>
<tr>
<td>
<%: Html.DisplayFor(modelItem => item.DateChecked) %>
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Shop.Name) %>
</td>
<td>
**<%: Html.DisplayFor(modelItem => item.Product.Name) %>**
</td>
<td>
**<%: Html.DisplayFor(modelItem => item.Product.ProductID) %>**
</td>
<td>
<%: Html.DisplayFor(modelItem => item.Active) %>
</td>
</tr>
<% } %>
</table>
スター付きのアイテムは空白で表示されます。
この問題を解決するために何をする必要があるかについてアドバイスをいただけますか? さらに情報が必要な場合は、お問い合わせください:)