This question already has an answer here:
这个问题在这里已有答案:
- The model item passed into the dictionary is of type .. but this dictionary requires a model item of type 4 answers
传递到字典中的模型项是类型..但是这个字典需要类型4答案的模型项
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)
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
和chnage代码如下
~/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 .
运行时,IIS有以下错误消息。
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'.
传递到字典中的模型项的类型为“ShoppingStore.WebUI.Models.ProductsListViewModel”,但此字典需要“ShoppingStore.WebUI.Models.MainPageModel”类型的模型项。
why have this error message
为什么会出现此错误消息
1 个解决方案
#1
1
Its seems you are returning wrong model to view "@model ShoppingStore.WebUI.Models.MainPageModel".
它似乎是你返回错误的模型来查看“@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)
}
#1
1
Its seems you are returning wrong model to view "@model ShoppingStore.WebUI.Models.MainPageModel".
它似乎是你返回错误的模型来查看“@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)
}