M
M
Michael2017-02-19 14:57:12
ASP.NET
Michael, 2017-02-19 14:57:12

How to correct parameter passing when calling View Model?

Hello, I have a class FurnitureVM , I use the View Model in my project, and I need to use the model of this class in my view, here is the view:

@model  FurnitureStore.ModelView.FurnitureVM
...
<div class="main container-fluid">

    @foreach (var p in Model.Furnitures)
    {
        <div class="product">

            @Html.Partial("Summary", p)
        </div>

    }

</div>

<div class="btn-group pull-right">
    @Html.PageLinks(Model.InfoPages, x => Url.Action("List", new { page = x , category = Model.CurrentCategory }))
</div>

And here is the controller for this view, which helps to display the products on the page, and also paginates the content:
public ViewResult List(string category, int page = 1)
        {
          
             FurnitureVM model = new FurnitureVM
             {
                 Furnitures = repository.Furnitures
                 .Where(p => category == null || p.Category.Name== category)
                 .OrderBy(f => f.FurnitureId)
                 .Skip((page - 1) * pageSize)
                 .Take(pageSize),
                 InfoPages = new InfoPage
                 {
                     CurrentPage = page,
                     ItemsPerPage = pageSize,
                     TotalItems =  category == null  ?  repository.Furnitures.Count() :
                     repository.Furnitures.Where(furniture => furniture.Category.Name == category).Count()
                 },
                 CurrentCategory = category
             };

            return View(model);
        }

and accordingly such fields in FurnitureVM
public IEnumerable<Furniture> Furnitures { get; set; }
        public IEnumerable<Category> Categories { get; set; }
        public InfoPageVM InfoPages { get; set; }
        public string CurrentCategory { get; set; }

But in the end I get an error when loading the List page, that the FurnitureVM type was expected in the model, but Furniture... was found, I understand that the error is in the FurnitureVM creation itself, where p.Category.Name== category, but how to fix it correctly ? Separately how to create an object, or how? Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question