我的MVC项目中出现了这样的错误:
'xxx.xxx.xxx.MarketplaceViewModel'不包含'MarketplaceCategories'的定义,并且没有扩展方法'MarketplaceCategories'接受类型'xxx.xxx.xxx.MarketplaceViewModel'的第一个参数可以找到(你错过了吗?) using指令或程序集引用?)
我试图在像这样布局的视图中访问它
@model xxx.xxx.xxx.MarketplaceViewModel
<div class="mp-options">
<span class="pull-left">
@foreach (var item in Model.MarketplaceCategories)
{
<a class="mp-category filter" data-filter=".category-1" href="#">@item.Name</a>
}
</span>
<a href="#" class="mp-icon mp-grid mp-selected" data-view="mp-view-grid">Grid View</a>
<a href="#" class="mp-icon mp-list" data-view="mp-view-list">List View</a>
</div>
<div id="app-container"><!--App Display-->
<div class="mix category-1" data-my-order="1">
<ul>
@foreach (var item in Model.MarketplaceItems)
{
<li>
<a class="mp-image mp-box-img" href="#">
<img src="@Url.Action("Image", new { id = @item.Id})" height="50">
</a>
<h3 class="mp-title">@item.Name</h3>
<div class="mp-price">@item.MinimumPrice.ToString("C") - @item.MaximumPrice.ToString("C")</div>
<div class="mp-details">
@item.Description
</div>
<a class="mp-icon mp-add" href="#">Add to cart</a>
</li>
}
</ul>
</div>
</div><!--/App Display-->
第二个foreach工作正常但Model.MarketplaceCategories
似乎是导致问题的那个。我上面的错误。这是模型
public class MarketplaceViewModel
{
public IEnumerable<MarketplaceCategory> MarketplaceCategories;
public List<MarketplaceItem> MarketplaceItems { get; set; }
}
MarketplaceCategory对象就像这样
public class MarketplaceCategory : xxxObject
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Description { get; set; }
}
我错过了什么会导致此错误?我还专门为类别
制作了另一个视图模型public class MarketplaceCategoryViewModel
{
public List<MarketplaceCategory> MarketplaceCategories { get; set; }
}
使用该模型会更容易吗?但后来我需要在一个页面上使用两个模型。
答案 0 :(得分:2)
MarketplaceCategories
是一个字段,而不是属性。将模型更改为
public class MarketplaceViewModel
{
public IEnumerable<MarketplaceCategory> MarketplaceCategories { get; set; } // add get and set
public List<MarketplaceItem> MarketplaceItems { get; set; }
}