我有一个 Controller 正在构建从 Linq 到 Sql 的查询以传递到 ViewBag.products 对象中。问题是,我无法像我预期的那样在其上使用 foreach 进行循环。
这是 Controller 中构建查询的代码,并应用了 .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 来包含 viewbag 循环。我已经尝试过使用和不使用部分并简单地使用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 = Awesomenes Game。但是,当我尝试执行此操作时,我收到错误“object”不包含“productName”的定义。
如何在循环中单独输出每个“字段”,以便我可以应用页面所需的正确 HTML 标签和样式?
我是否需要创建一个全新的 ViewModel 来执行此操作,然后创建一个显示模板,如下所示:'object' does not contain a definition for 'X'
或者我可以做我在这里尝试的事情吗?
*****更新*****
在我的 Controller 中,我现在有以下内容:
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; }
}
}
我的 View 现在有
@model IEnumerable<JustBundleIt.Models.FullBundleModel>
@foreach (var item in Model)
{
<div class="hp_bundle">
<h3>@Html.Display(item.bundleName)</h3>
</div>
}
如果我从模型引用中删除 IEnumerable,foreach 会出错,指出枚举器没有公共(public)定义。
在@Html.Display(item.bundleName)中,它错误地指出该模型没有bundleName的定义。如果我尝试
@foreach(var item in Model.bundleItems)
我收到一条错误,表明模型中未定义bundleItems。
那么我没有正确连接以使用组合模型吗?
最佳答案
Do I need to make a whole new ViewModel to do this, and then create a display template as referenced here...
Darin's answer您链接的说明了重要的概念:Anonymous types are not intended for use across assembly boundaries并且 Razor 不可用。我想补充一点,在直接上下文之外公开匿名对象并不是一个好主意。
专门为 View 消费创建 View 模型几乎总是正确的方法。如果您呈现相似的数据,则 View 模型可以在 View 之间重用。
没有必要创建显示模板,但如果您想重用显示逻辑,它会很有用。 HTML 助手还可以提供类似的功能,提供可重用的显示逻辑。
话虽如此,将匿名类型传递给 View 或读取匿名类型的成员也并非不可能。 RouteValueDictionary 可以采用匿名类型(甚至跨程序集)并读取其属性。反射使得无论可见性如何都可以读取成员。虽然这有其用途,但将数据传递到 View 不是其中之一。
更多阅读:
关于asp.net-mvc-3 - Foreach ViewBag 数据给出 'object' 不包含 'var' 的定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12469475/