Muhammad Arslan Jamshaid
Muhammad Arslan Jamshaid

Reputation: 1197

Pass IEnumerable ViewModel to View

I have two Tables, Product & ProductImages

+---PRODUCT----+
| ID           |
| Name         |
```````````````

+---PRODUCT Images----+
| ID                  |
| ProductId           |
| URL                 |
```````````````````````

I want to display the data in my Index View from both the tables via ViewModel

Here are my ViewModel

public class ProductDisplayViewModel{
    public int ProductId { get; set; }
    public string Name {get; set;}        
    public string ImageUrl { get; set; }

}

public class ProductIenumViewModel
{
    public IEnumerable<ProductDisplayViewModel> productDisplayViewModel { get; set; }
}

That's my Controller

 public ActionResult Index()
    {

        var product = new ProductDisplayViewModel
        {
            //ProductId = db.Products.Select(s => s.Id).ToString,
            Name = db.Products.Select(s => s.Name).ToString(),
            ImageUrl = db.ProductImages.Select(s => s.URL).ToString()
        };


        var productIenumViewModel = new ProductIenumViewModel
        {
            productDisplayViewModel =  new List<ProductDisplayViewModel> { product }
        };

        return View(productIenumViewModel);
    }

But I get the error that I have passed ViewModel.ProductIenumViewModel but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[ViewModel.ProductDisplayViewModel]'

if I use productDisplayViewModel = IEnumerable<ProductDisplayViewModel> product in my Controller I get the error that ProductDisplayViewModel is a type but is used like variable

Edit This is my View

@model IEnumerable<MyApp.Areas.Admin.ViewModel.ProductDisplayViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table table-bordered table-striped" >
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>      
        <th>
            @Html.DisplayNameFor(model => model.ImageUrl)
        </th>  
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
            @Html.DisplayFor(modelItem => item.ImageUrl)
        </td>

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ProductId }) |
            @Html.ActionLink("Details", "Details", new { id=item.ProductId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ProductId })
        </td>
    </tr>
}

</table>

Upvotes: 3

Views: 9874

Answers (1)

JamieD77
JamieD77

Reputation: 13949

Besides the error you're getting, you're not forming your IEnumerable<ProductDisplayViewModel> correctly.

Your controller code should look something like this assuming that your view has a @model IEnumerable<ProductDisplayViewModel>

public ActionResult Index()
{

    var products = db.ProductImages
                     .Include("Product")
                     .Select(a => new ProductDisplayViewModel {
                          ProductId = a.ProductID,
                          Name = a.Product.Name,
                          ImageUrl = a.URL
                     });
    return View(products );
}

Upvotes: 7

Related Questions