【发布时间】:2016-01-28 11:16:19
【问题描述】:
我有以下模型类
public class ProductViewModel
{
public Product Products { get; set; }
public IEnumerable<Product> LProducts { get; set; }
}
public partial class Product
{
public string id { get; set; }
public string detail { get; set; }
}
然后我对 List 和 Create 都有以下视图
@model albaraka.Models.ProductViewModel
<table class="table">
<tr>
<th>
ID
</th>
<th>
Detail
</th>
</tr>
@foreach (var obj in Model.LProducts)
{
<tr>
<td>
@Html.DisplayFor(modelItem => obj.id)
</td>
<td>
@Html.DisplayFor(modelItem => obj.detail)
</td>
</tr>
}
</table>
<div>
@using ())
{
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.id)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Products.id)
@Html.ValidationMessageFor(m => m.Products.id)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Products.detail)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Products.detail)
@Html.ValidationMessageFor(m => m.Products.detail)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
</div>
}
</div>
@section Scripts
{}
这是列表的控制器方法
[HttpGet]
public ViewResult Product_List()
{
ProductViewModel viewModelList = new ProductViewModel();
viewModelList.LProducts = from products in db.Product
where products.details == "Pending"
select products;
return View(viewModelList.LProducts.ToList());
}
但在这里我收到以下错误
传入字典的模型项是类型 'System.Collections.Generic.List`1[projectname.Models.Product]',但是 这个字典需要一个类型的模型项 'projectname.Models.ProductViewModel'。
【问题讨论】:
标签: c# .net asp.net-mvc linq asp.net-mvc-4