【发布时间】:2011-08-01 05:45:29
【问题描述】:
我已经研究过这个问题,但找不到解决方案。我的问题是,在我看来,我无法查看我的 ViewModel 的属性(例如,Model.Supplier.SupplerName)。 Intellisense 只允许我使用 Model.Suppliers 或 Model.ToolTypes 或 Model.ToolTypesSuppliers 。我无法获取包含在 ViewModel 中的每个模型的属性。
视图模型:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ToolOrder.Models
{
public class ToolTypeSupplierViewModel
{
public ToolTypeSupplierViewModel(IEnumerable<ToolType> tooltypes, IEnumerable<Supplier> suppliers, IEnumerable<ToolTypeSupplier> tooltypesuppliers)
{
this.ToolTypes = tooltypes;
this.Suppliers = suppliers;
this.ToolTypeSuppliers = tooltypesuppliers;
}
public IEnumerable<ToolType> ToolTypes { get; private set; }
public IEnumerable<Supplier> Suppliers { get; private set; }
public IEnumerable<ToolTypeSupplier> ToolTypeSuppliers { get; private set; }
}
}
控制器:
public ActionResult ListToolTypeSupplierOptions()
{
var _tooltypelist =_repository.FetchAllToolTypeOptions();
var _supplierlist = _repository.FetchAllSupplierOptions();
var _toolstypesupplierlist = _repository.FetchAllToolTypeSupplierOptions();
return View(new ToolTypeSupplierViewModel(_tooltypelist, _supplierlist, _toolstypesupplierlist));
}
查看:
@model IEnumerable<ToolOrder.Models.ToolTypeSupplierViewModel>
<table>
<tr>
<th>Supplier</th>
<th>Tool TYpe</th>
<th>Created</th>
<th>Last Modified </th>
</tr>
@foreach (var item in Model.) {
<tr>
<td>
</td>
</tr>
}
</table>
任何帮助将不胜感激,或者如果需要进一步澄清,我可以跟进。如果这有点含糊,我深表歉意,我一直在努力弄清楚为什么我不能提取我包含在我的 ViewModel 中的每个模型的属性。提前谢谢你。
【问题讨论】:
-
您是想在
foreach语句中的Model或item的循环中查看Intellisense 属性吗?在foreach的声明中,您只会获得针对集合类型的 Intellisense。然后在循环中,如果您执行了item.,那么您应该会看到您选择的任何集合类型的属性。 -
@munozca 一个总的其他话题,但一个提示:我会使用一个门面层来调用你的 3 个存储库方法。然后你的外观层会返回一个数据传输对象给富客户端:)
标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-3 viewmodel