如何在视图中结合两个模型(ASP.net MVC)

时间:2017-06-28 07:31:01

标签: c# asp.net asp.net-mvc asp.net-mvc-4

视图中存在两个模型

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);
}
我用这种方式 Multiple models in a view

并在一个模型中添加两​​个模型

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; }
    }
}

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

    }

运行时,IIS出现以下错误消息。

传入字典的模型项类型为&#39; ShoppingStore.WebUI.Models.ProductsListViewModel&#39;,但此字典需要类型为&#39; ShoppingStore.WebUI.Models的模型项。 MainPageModel&#39;

为什么会出现此错误消息

1 个答案:

答案 0 :(得分:1)

似乎你回来了错误的模型来查看“ @model ShoppingStore.WebUI.Models.MainPageModel ”。

您的代码看起来不错,但您需要检查您的控制器返回视图详细信息,它应该是:

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)

}