【发布时间】:2012-09-10 06:32:10
【问题描述】:
我有一个控制器正在构建从 Linq 到 Sql 的查询以传递到 ViewBag.products 对象。问题是,我无法像预期的那样循环使用 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 来包含 viewbag 循环。我已经尝试过使用和不使用部分的相同结果,并且只需使用 index.cshtml 中的 foreach。包含部分的 sn-p 如下:
<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。但是,当我尝试此操作时,我收到错误“对象”不包含“产品名称”的定义。
如何在循环中单独输出每个“字段”,以便我可以应用适当的 HTML 标记和页面所需的样式?
我是否需要制作一个全新的 ViewModel 来执行此操作,然后创建一个显示模板,如下所示:'object' does not contain a definition for '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。
那么我没有正确连接以使用组合模型?
【问题讨论】:
-
第一:不要将您的模型定义为 IEnumerable。只需使用
@model JustBundleIt.Models.FullBundleModel。第二:将 for 循环更改为@foreach(var item in Model.bundleItems),将 HTML 助手更改为@Html.DisplayFor(modelItem => item.bundleName)。 -
谢谢。这就是我可以说我今天已经坚持了太久的方式。非常感谢。
-
大声笑,没问题。我们都去过那里。在我们的世界上做事的方式通常不止一种,这无济于事。您可能可以通过 100 多种不同的方式来解决这个问题,但最终还是会得到相同的结果。
标签: asp.net-mvc-3 razor viewbag