Asp net mvc 3 using viewmodel error that viewmodel is not ienumerable


ASP.NET is a popular programming used for building web . It provides a framework for creating dynamic websites, web , and web applications. One of the common issues faced by developers while working with ASP.NET is the error “ViewModel is not IEnumerable”. In this article, we will explore this error and provide a solution with .

Understanding the Error

The error “ViewModel is not IEnumerable” typically occurs when ASP.NET MVC 3 and working with . ViewModels are used to pass data from the controller to the view. In some cases, when trying to pass a ViewModel to the view, you may encounter this error.


To solve the “ViewModel is not IEnumerable” error, you need to ensure that your ViewModel implements the IEnumerable interface. This interface allows you to iterate over a of objects. By implementing this interface, you can pass the ViewModel as an IEnumerable to the view.


Let's consider an example where we have a ViewModel called “ProductViewModel” that represents a collection of products. We want to pass this ViewModel to the view and display the products.

public class ProductViewModel : IEnumerable
    public List Products { get; set; }

    public IEnumerator GetEnumerator()
        return Products.GetEnumerator();

public class Product
    public string Name { get; set; }
    public decimal Price { get; set; }

In the above example, the ProductViewModel implements the IEnumerable interface and provides an for the GetEnumerator method. This method returns the enumerator for the Products collection.

Now, let's see how we can use this ViewModel in the controller and pass it to the view:

public class ProductController : Controller
    public ActionResult Index()
        var products = new List
            new Product { Name = "Product 1", Price = 10.99m },
            new Product { Name = "Product 2", Price = 19.99m },
            new Product { Name = "Product 3", Price = 5.99m }

        var viewModel = new ProductViewModel { Products = products };

        return View(viewModel);

In the above code, we create a list of products and the ProductViewModel with this list. We then pass the viewModel to the view using the View method.

Finally, in the view, you can iterate over the products using a foreach loop:

@model ProductViewModel

@foreach (var product in Model)

@product.Name - $@product.Price


In the above code, we use the @model to specify the type of the model. We then iterate over the Model, which is of type ProductViewModel, and display the name and price of each product.


The “ViewModel is not IEnumerable” error in ASP.NET MVC 3 can be resolved by implementing the IEnumerable interface in your ViewModel. By doing so, you can pass the ViewModel as an IEnumerable to the view and iterate over its collection of objects. The provided example demonstrates how to implement and use a ViewModel that implements the IEnumerable interface.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents