简体   繁体   English

将IEnumerable ViewModel传递给View

[英]Pass IEnumerable ViewModel to View

I have two Tables, Product & ProductImages 我有两个表, ProductProductImages

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

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

I want to display the data in my Index View from both the tables via ViewModel 我想通过ViewModel在两个表中的Index View显示数据

Here are my ViewModel 这是我的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]' 但是我得到了我已经传递ViewModel.ProductIenumViewModel的错误,但是此字典需要类型为'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 如果我在控制器中使用productDisplayViewModel = IEnumerable<ProductDisplayViewModel> product ,则会收到以下错误: ProductDisplayViewModel是一种类型,但像变量一样使用

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>

Besides the error you're getting, you're not forming your IEnumerable<ProductDisplayViewModel> correctly. 除了得到的错误之外,您还没有正确形成IEnumerable<ProductDisplayViewModel>

Your controller code should look something like this assuming that your view has a @model IEnumerable<ProductDisplayViewModel> 假设您的视图具有@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 );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM