简体   繁体   中英

Web model does not contain a definition in asp.net mvc

I'm getting an error in my MVC project that goes like this:

'xxx.xxx.xxx.MarketplaceViewModel' does not contain a definition for 'MarketplaceCategories' and no extension method 'MarketplaceCategories' accepting a first argument of type 'xxx.xxx.xxx.MarketplaceViewModel' could be found (are you missing a using directive or an assembly reference?)

I am trying to access it in a view that is laid out like this

@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-->

The second foreach works fine but the Model.MarketplaceCategories seems to be the one that is causing problems. I get the errror above. This is the model

public class MarketplaceViewModel
{
    public IEnumerable<MarketplaceCategory> MarketplaceCategories;
    public List<MarketplaceItem> MarketplaceItems { get; set; }
}

The MarketplaceCategory objects are like this

public class MarketplaceCategory : xxxObject
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Description { get; set; }
}

What am i missing that would cause this error? I also made another view model specifically for the categories

public class MarketplaceCategoryViewModel
{
    public List<MarketplaceCategory> MarketplaceCategories { get; set; } 
}

Would it be easier to use that model instead? But then i'd need two models on one page.

MarketplaceCategories is a field, not a property. Change your model to

public class MarketplaceViewModel
{
  public IEnumerable<MarketplaceCategory> MarketplaceCategories { get; set; } // add get and set
  public List<MarketplaceItem> MarketplaceItems { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM