c# - 如何使用 ViewModel 在 MVC4 应用程序中实现 IEnumerator

标签 c# asp.net-mvc-4 mvvm ienumerator

我正在尝试按照 this tutorial 在我的 MVC4 项目的一个 View 中返回两个模型.我有一个名为 Product 的模型,如下所示:

public class Product : IEnumerable<ShoppingCartViewModel>, 
                           IList<ShoppingCartViewModel>
    {
        public int ProductId { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        (...)
    }

还有一个带有 ShoppingCarts (List) 列表的 ViewModel,如下所示:
public class ShoppingCartViewModel : IEnumerable<Product>, IList<Product>
    {
        public List<Cart> CartItems { get; set; }
        public decimal CartTotal { get; set; }
    }

我有一个“包装模型”,它执行以下操作:
public class ProductAndCartWrapperModel
    {
        public Product product;
        public ShoppingCartViewModel shoppingCart;

        public ProductAndCartWrapperModel()
        {
            product = new Product();
            shoppingCart = new ShoppingCartViewModel();
        }
}

然后我尝试以这种方式简单地显示具有两种不同模型的 View
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<projectname.ProductAndCartWrapperModel>" %>
(...)
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
        <% foreach (projectname.Models.Product p in
                                     ViewData.Model.product) { %>
        <div>
            <div id="ProductName"><%: p.Name %></div>
            <div id="ProductPrice"><%: p.Price %></div>
        </div>
        <% } %>
    </div>
    <div>
        <% foreach (projectname.ViewModels.ShoppingCartViewModel sc in 
                   ViewData.Model.shoppingCart) { %>
        <div>
            <div id="Div1"><%: sc.CartItems %></div>
            <div id="Div2"><%: sc.CartTotal %></div>
        </div>
        <% } %>
    </div>

</asp:Content>

不幸的是,在尝试构建时出现一个错误
Cannot convert type 'projectname.Models.Product' to
'projectname.ViewModels.ShoppingCartViewModel'

随后是两个模型的错误列表,如下所示:
does not implement interface member 
'System.Collections.Generic.IEnumerable<projectname.ViewModels.ShoppingCartViewModel>.
 GetEn umerator()'. 'projectname.Models.Product.GetEnumerator()' cannot implement  
'System.Collections.Generic.IEnumerable<projectname.ViewModels.ShoppingCartViewModel>.
 GetEnumerator()' because it does not have the matching return type of 
'System.Collections.Generic.IEnumerator<projectname.ViewModels.ShoppingCartViewModel>'.

我觉得我非常接近在一个页面上显示这两个模型,但我不知道如何实现 IEnumerator 并获得匹配的类型。我试图添加一个这样的:
public IEnumerator<Object> GetEnumerator()
    {
        return this.GetEnumerator();
    }

但这无济于事。

如果有人能解释如何正确实现接口(interface)成员并获得构建解决方案(如果可能),我将不胜感激。

最佳答案

Product继承自 IEnumerable<ShoppingCartViewModel> ,这意味着迭代 Product会给ShoppingCartViewModel元素。

<% foreach (Product p in ViewData.Model.product) { %>

应该 :
<% foreach (ShoppingCartViewModel p in ViewData.Model.product) { %>

但是你这里的设计很奇怪,也许你做得过火了?

关于c# - 如何使用 ViewModel 在 MVC4 应用程序中实现 IEnumerator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19688431/

相关文章:

c# - IRS-A2A BulkRequestTransmitter消息格式不正确和/或无法解释

c# - 不支持 URI 前缀

javascript - 如何将动态创建的超链接的 css 值传递给另一个页面

.net - Caliburn.Micro:XAML 中的不同 "Targets"以及如何将 View / View 模型放入其中

c# - c# 解决方案中的命名空间和文件夹结构 : how should folders on disk be organised?

c# - 如何使套接字客户端准备好发送和接收而不需要服务器的操作?

c# - ASP.NET MVC 4 中 Controllers 目录中的文件夹

asp.net - 将 OpenIDConnect 集成到经典 ASP 应用程序中

c# - 如何将 X 按钮绑定(bind)到窗口的关闭按钮

c# - 使用 MVVM 的 wpf 中的案例约定