我已經研究過這個問題,找不到解決方案。我的問題是,在我看來,我無法查看我的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