-1

Two model have combined to exist in view

1.~/Products.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShoppingStore.Domain.Entities
{
    public class Product
    {
        public int ProductID { get; set;}
        public string Name { get; set; }
        public string Description { get; set; }
        public decimal Price { get; set;  }
        public string Category { get; set; }
    }
}

2.~/PorductListViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using ShoppingStore.Domain.Entities;

namespace ShoppingStore.WebUI.Models
{
    public class ProductsListViewModel
    {
        public IEnumerable<Product> Products { get; set; }
        public PagingInfo PageInfo { get; set; }
        public string CurrentCategory { get; set; }
    }
}

3.control ~/ProductController.cs public ViewResult List(string category , int page=1)

{
            ProductsListViewModel model = new ProductsListViewModel
            {
               Products = repository.Products.Where(p => category ==null || p.Category ==category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize),
                PageInfo = new PagingInfo {CurentPage = page,ItemPerPage = PageSize,TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count()},
                CurrentCategory = category
            }; 
            return View(model);
}

i use this way Multiple models in a view

and add two model in one model

1.~/MainPageModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ShoppingStore.Domain.Entities;

namespace ShoppingStore.WebUI.Models
{
    public class MainPageModel
    {
        public ProductsListViewModel ProductsListViewModel { get; set; }
        public Product Product { get; set; }
    }
}

and chnage code like below

~/List.cshtml

@model ShoppingStore.WebUI.Models.MainPageModel

@{
    ViewBag.Title = "Prodcuts";

}
@foreach (var p in Model.PorductListViewModel.Products)
{

 @Html.Partial("ProductSummary",p)
}


<div>
    @Html.PageLinks(Model.PorductListViewModel.PageInfo, x => Url.Action("List", new { page = x , category = Model.PorductListViewModel.CurrentCategory }))
</div>

<div>
         @using (Html.BeginForm())
         {

            <div >
                @Html.HiddenFor(x => x.Product.ProductID)
                @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
                <input type="submit" value="Add to cart" />
            </div>

            <div>
                <div id='DIV2'><input type="button" onclick="incrementValue1()" value="ADD" /></div>
                <div id="DIV2"> <input type="text" id="number1" value="0" /></div>
                <div id="DIV2"><input type="button" onclick="decrementValue1()" value="minus" /></div>
            </div>
            <button id="checkvalue" onclick="checkValue()" type="submit">Check</button>
         </div>

    }

When Running , IIS have below error message .

The model item passed into the dictionary is of type 'ShoppingStore.WebUI.Models.ProductsListViewModel', but this dictionary requires a model item of type 'ShoppingStore.WebUI.Models.MainPageModel'.

why have this error message

square
  • 123
  • 1
  • 1
  • 11
  • How do you call the view? From a controller? From other view? Show us all the relevant code. But the problem is easy, you must be doing something like `return View("List", new ProductsListViewModel())` instead of `return View("List", new MainPageModel())` – Rumpelstinsk Jun 28 '17 at 07:35
  • Share your controller return View() details – Laxman Gite Jun 28 '17 at 07:36
  • 2
    The error is self explanatory really the model required is ShoppingStore.WebUI.Models.MainPageModel and you are using ShoppingStore.WebUI.Models.ProductsListViewModel in your view, Change the model passed in by the controller and do some kinds of mapping – Jack M Jun 28 '17 at 07:38
  • i add my control , thx for your replay – square Jun 28 '17 at 08:42

1 Answers1

1

Its seems you are returning wrong model to view "@model ShoppingStore.WebUI.Models.MainPageModel".

Your Code looks good but you need to check your controller return view details its should be like :

public ActionResult ViewAction()
{

MainPageModel objMainPageModel = new MainPageModel();
objMainPageModel.ProductsListViewModel = new ProductsListViewModel();
objMainPageModel.ProductsListViewModel= //Bind this model as well

//Bind Data in objMainPageModel and return to view
return View("List", objMainPageModel)

}
Laxman Gite
  • 2,248
  • 2
  • 15
  • 23