使用ViewModel查看

时间:2014-11-18 19:46:10

标签: asp.net-mvc-3 asp.net-mvc-4 razor viewmodel

我有一个ViewModel,其中包含其他ViewModels

public class AllProductsViewModel
{
    public ICollection<ProductSearchViewModel> ProductSearch { get; set; }

    public ICollection<ProductRentViewModel> ProductRent { get; set; }

    public ICollection<ProductBuyViewModel> ProductBuy { get; set; }
}

我的控制器是:

    public ActionResult Index()
    {
        var listOfProductsBuy = db.ProductsBuy.Select(x => new ProductBuyViewModel
        {

            Id = x.Id,
            Title = x.Title,
            MasterImageUrl = x.Images.FirstOrDefault().Url,
            Price = x.Price,
            Values = x.Value,

        }).ToList();

        var listOfProductsRent = db.ProductsRent.Select(y => new ProductRentViewModel
            {
                Id = y.Id,
                Title = y.Title,
                MasterImageUrl = y.ImagesRent.FirstOrDefault().Url,
                Price = y.Price,
                Values = y.Value,
            }).ToList();

        var listOfProductsSearch = db.ProductSearches.Select(z => new ProductSearchViewModel
        {
            Id = z.Id,
            Title = z.Title,
            MasterImageUrl = z.ImagesSearch.FirstOrDefault().Url,
            Price = z.Price,
            Values = z.Value,
        }).ToList();

        var viewModel = new AllProductsViewModel { ProductBuy = listOfProductsBuy, ProductRent = listOfProductsRent, ProductSearch = listOfProductsSearch };
        return View(viewModel);
    }
}

我的观点:

@model IEnumerable<RealEstateMarket.ViewModels.AllProductsViewModel>
@{
    ViewBag.Title = "Home Page";
}

<br />
<div class="row">
        <div class="col-md-3">
            @foreach (var item in Model)
            {
                <h3>@item.ProductBuy.Select(x => x.Title)</h3>
                <div>
                    <img height="100" width="120" class="thumbnail" src="@item.ProductBuy.Select(x => x.MasterImageUrl )" />
                </div>
                <h3>@item.ProductBuy.Select(x => x.Price) @item.ProductBuy.Select(x => x.Values.Currency)</h3>
            }        
        </div>
</div>

想要获取ProductBuyViewModel,ProductRentViewModel,ProductSearchViewModel的字段 当我启动此代码时,我收到错误:传递到字典中的模型项的类型为&#39; RealEstateMarket.ViewModels.AllProductsViewModel&#39;,但此字典需要类型为&#39; System.Collections的模型项。 Generic.IEnumerable`1 [RealEstateMarket.ViewModels.AllProductsViewModel]&#39;

1 个答案:

答案 0 :(得分:2)

更改此

 @model IEnumerable<RealEstateMarket.ViewModels.AllProductsViewModel>

 @model RealEstateMarket.ViewModels.AllProductsViewModel

然后您就可以访问您的列表

    <div class="col-md-3">
        @for (int index=0;index<Model.ProductSearch.Count();index++)
        {
            <h3>@Model.ProductSearch[index].Title</h3>
            <div>
                <img height="100" width="120" class="thumbnail" src="@Model.ProductSearch[index].MasterImageUrl )" />
            </div>
            <h3>@Model.ProductSearch[index].Price @Model.ProductSearch[index].Values.Currency)</h3>
        }        
    </div>