How to implement IEnumerator in MVC4 application with ViewModel

Erwin Rooijakkers

I am trying to return two models in one view in my MVC4 project by following this tutorial. I have one Model named Product that looks like this:

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

And a ViewModel with a list of ShoppingCarts (List) that looks like this:

public class ShoppingCartViewModel : IEnumerable<Product>, IList<Product>
    {
        public List<Cart> CartItems { get; set; }
        public decimal CartTotal { get; set; }
    }

I have one "wrapper model" that does the following:

public class ProductAndCartWrapperModel
    {
        public Product product;
        public ShoppingCartViewModel shoppingCart;

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

Then I try to simply display the view with the two different models in this way

<%@ 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>

Unfortunately, when trying to build I get one error

Cannot convert type 'projectname.Models.Product' to
'projectname.ViewModels.ShoppingCartViewModel'

followed by a list of errors that look like this for both the models:

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>'.

I have the feeling I am quite close to displaying both of the models on one page, but I do not know how to implement the IEnumerator and get the types to match. I tried to add one like this:

public IEnumerator<Object> GetEnumerator()
    {
        return this.GetEnumerator();
    }

but that was to no avail.

I would be very grateful if someone could explain how to correctly implement the interface members and get the solution to build (if possible).

Réda Mattar

When Product inherits from IEnumerable<ShoppingCartViewModel>, it means that iterating over Product will give ShoppingCartViewModel elements.

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

should be :

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

But your design here is quite weird, maybe are you overdoing it ?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to initialize viewmodel in mvc4

From Dev

how to implement datetimepicker in mvc4

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

How to mock method with ViewModel in MVC4 with Entity Framework 4.0

From Dev

MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

From Dev

How to Implement Dependent Dropdownlist in MVC4 Razor

From Dev

How to Mock MVC4 application when UI testing with Watin?

From Dev

How to implement Login using Membership provider into MVC 4 application ?

From Dev

How can i use Dictionary in MVC4 ViewModel,Controller, View?

From Dev

How do I pass a ViewModel to an Edit Action in ASP.NET MVC4

From Dev

Outputcache in MVC4 Application

From Dev

How to set up IIS6 to host ASP .NET 4 MVC4 application

From Dev

IEnumerator does not implement interface IEnumerable

From Dev

Where authorization should implement in MVC4 ?

From Dev

How to Implement "extends Application"?

From Dev

How to override Browser.IsMobileDevice is ASP.NET MVC4 application?

From Dev

How to profile MVC4 application, send multiple get requests to Web site for testing

From Dev

How can I speed up my MVC4 application DEVELOPMENT?

From Dev

How to force Motorola 2180 mobile computer to be recognized as mobile device in ASP.NET MVC4 application

From Dev

How to enable store virtual directory and Store controller in MVC4 application in Windows 2003 server

From Dev

How to run a asp.net MVC4 web application in RedHat OS

From Dev

How to profile MVC4 application, send multiple get requests to Web site for testing

From Java

How to implement validation using ViewModel and Databinding?

From Dev

How to implement SignalR to show email notifications to receivers in ASP.NET MVC 4 Application

From Dev

How to Implement OWIN and Katana in MVC 5Application

From Dev

How to implement virus scan on file upload in a Spring 3 MVC application

From Dev

How to implement virus scan on file upload in a Spring 3 MVC application

From Dev

MVC4 Application on Azure not working

From Dev

MVC4 Application on Azure not working

Related Related

  1. 1

    How to initialize viewmodel in mvc4

  2. 2

    how to implement datetimepicker in mvc4

  3. 3

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  4. 4

    How to mock method with ViewModel in MVC4 with Entity Framework 4.0

  5. 5

    MVC4 app - how to remove Entity Framework and implement SimpleMembershipProvider?

  6. 6

    How to Implement Dependent Dropdownlist in MVC4 Razor

  7. 7

    How to Mock MVC4 application when UI testing with Watin?

  8. 8

    How to implement Login using Membership provider into MVC 4 application ?

  9. 9

    How can i use Dictionary in MVC4 ViewModel,Controller, View?

  10. 10

    How do I pass a ViewModel to an Edit Action in ASP.NET MVC4

  11. 11

    Outputcache in MVC4 Application

  12. 12

    How to set up IIS6 to host ASP .NET 4 MVC4 application

  13. 13

    IEnumerator does not implement interface IEnumerable

  14. 14

    Where authorization should implement in MVC4 ?

  15. 15

    How to Implement "extends Application"?

  16. 16

    How to override Browser.IsMobileDevice is ASP.NET MVC4 application?

  17. 17

    How to profile MVC4 application, send multiple get requests to Web site for testing

  18. 18

    How can I speed up my MVC4 application DEVELOPMENT?

  19. 19

    How to force Motorola 2180 mobile computer to be recognized as mobile device in ASP.NET MVC4 application

  20. 20

    How to enable store virtual directory and Store controller in MVC4 application in Windows 2003 server

  21. 21

    How to run a asp.net MVC4 web application in RedHat OS

  22. 22

    How to profile MVC4 application, send multiple get requests to Web site for testing

  23. 23

    How to implement validation using ViewModel and Databinding?

  24. 24

    How to implement SignalR to show email notifications to receivers in ASP.NET MVC 4 Application

  25. 25

    How to Implement OWIN and Katana in MVC 5Application

  26. 26

    How to implement virus scan on file upload in a Spring 3 MVC application

  27. 27

    How to implement virus scan on file upload in a Spring 3 MVC application

  28. 28

    MVC4 Application on Azure not working

  29. 29

    MVC4 Application on Azure not working

HotTag

Archive