Muh*_*aid 3 c# asp.net-mvc mvvm asp.net-mvc-4
我有两张桌子,Product&ProductImages
+---PRODUCT----+
| ID |
| Name |
```````````````
+---PRODUCT Images----+
| ID |
| ProductId |
| URL |
```````````````````````
Run Code Online (Sandbox Code Playgroud)
我想Index View通过两个表显示我的数据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; }
}
Run Code Online (Sandbox Code Playgroud)
那是我的控制器
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);
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了我已经通过的错误,ViewModel.ProductIenumViewModel但是这本字典需要一个类型为的模型项'System.Collections.Generic.IEnumerable 1[ViewModel.ProductDisplayViewModel]'
如果我productDisplayViewModel = IEnumerable<ProductDisplayViewModel> product在我的控制器中使用,我会得到ProductDisplayViewModel一个类型的错误,但像变量一样使用
编辑 这是我的观点
@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>
Run Code Online (Sandbox Code Playgroud)
除了你得到的错误,你没有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 );
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7882 次 |
| 最近记录: |