私は、ViewBag.productsオブジェクトに渡すためにLinqからSQLへのクエリを構築しているコントローラーを持っています。問題は、予想どおりにforeachを使用してループできないことです。
.ToList()関数が適用された、クエリを構築するコントローラーのコードは次のとおりです。
var products = from bundles in db.Bundle
join bProducts in db.BundleProducts on bundles.bundleId equals bProducts.bundleID
join product in db.Products on bProducts.productID equals product.productID
join images in db.Images on product.productID equals images.productID
where bundles.bundleInactiveDate > DateTime.Now
select new {
product.productName,
product.productExcerpt,
images.imageID,
images.imageURL
};
ViewBag.products = products.ToList();
Index.cshtmlで必要な他のアイテムに別のモデルを使用しているので、単純なHtml.Partialを使用してビューバッグループを含めることができると思いました。パーシャルを使用する場合と使用しない場合で、index.cshtmlのforeachを使用するだけで、同じ結果で試してみました。パーシャルを含むスニペットは以下のとおりです。
<div id="bundle_products">
<!--build out individual product icons/descriptions here--->
@Html.Partial("_homeBundle")
</div>
私の_homeBundle.cshtmlファイルには、次のものがあります。
@foreach (var item in ViewBag.products)
{
@item
}
ViewBagデータを取得していますが、リスト全体を出力として取得しています。
{ productName = Awesomenes Game, productExcerpt = <b>Awesome game dude!</b>, imageID = 13, imageURL = HotWallpapers.me - 008.jpg }{ productName = RPG Strategy Game, productExcerpt = <i>Test product excerpt</i>, imageID = 14, imageURL = HotWallpapers.me - 014.jpg }
私にできると思ったのは:
@foreach(var item in ViewBag.Products)
{
@item.productName
}
ご覧のとおり、出力では、productName =AwesomenesGameです。ただし、これを実行しようとすると、「object」に「productName」の定義が含まれていないというエラーが表示されます。
ページに必要な適切なHTMLタグとスタイルを適用できるように、ループ内で個別に言うように、各「フィールド」を出力するにはどうすればよいですか?
これを行うには、まったく新しいViewModelを作成してから、ここで参照されているように表示テンプレートを作成する必要がありますか?'object'には'X'の定義が含まれていません
または、ここで試みていることを実行できますか?
*****アップデート*****
私のコントローラーには、次のものがあります。
var bundle = db.Bundle.Where(a => a.bundleInactiveDate > DateTime.Now);
var products = from bundles in db.Bundle
join bProducts in db.BundleProducts on bundles.bundleId equals bProducts.bundleID
join product in db.Products on bProducts.productID equals product.productID
join images in db.Images on product.productID equals images.productID
where bundles.bundleInactiveDate > DateTime.Now
select new {
product.productName,
product.productExcerpt,
images.imageID,
images.imageURL
};
var bundleContainer = new FullBundleModel();
bundleContainer.bundleItems = bundle;
return View(bundleContainer);
私はモデル、FullBundleModelを持っています
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace JustBundleIt.Models
{
public class FullBundleModel
{
public IQueryable<Bundles> bundleItems { get; set; }
public IQueryable<Images> imageItems { get; set; }
}
}
そして私のビューは今持っています
@model IEnumerable<JustBundleIt.Models.FullBundleModel>
@foreach (var item in Model)
{
<div class="hp_bundle">
<h3>@Html.Display(item.bundleName)</h3>
</div>
}
モデル参照からIEnumerableを削除すると、foreachは、列挙子のパブリック定義がないというエラーを出します。
@ Html.Display(item.bundleName)で、モデルにbundleNameの定義がないというエラーが発生します。しようとしたら
@foreach(var item in Model.bundleItems)
bundleItemsがモデルで定義されていないというエラーが表示されます。
では、組み合わせたモデルを使用するために正しく配線されていないのは何ですか?