我已经研究过这个问题,找不到解决方案。我的问题是,在我看来,我无法查看我的ViewModel的属性(例如,Model.Supplier.SupplerName)。 Intellisense只允许我使用Model.Suppliers或Model.ToolTypes或Model.ToolTypesSuppliers。我无法获取ViewModel中包含的每个模型的属性。MVC 3 ViewModel模式问题(使用RAZOR)
视图模型:
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.',那么你应该看到你选择的集合类型的属性。 – 2011-04-08 05:33:11
@munozca一个总的其他话题,但一个提示:我会使用一个Facade层调用你的3个仓库方法。然后你的外观层将返回一个数据传输对象到富客户端:) – Elisabeth 2013-02-03 14:12:43