如何在ASP.NET MVC 4 Razor项目中显示集合?

我有以下型号:

public class ContractPlain { public int Id { get; set; } public Guid ContractGuid { get; set; } public int SenderId { get; set; } public int RecvId { get; set; } public int ContractType { get; set; } public string ContractStatus { get; set; } public DateTime CreatedTime { get; set; } public DateTime CreditEnd { get; set; } } public class Contrtacts { List listOutput; public void Build(List listInput) { listOutput = new List(); } public List GetContracts() { return listOutput; } internal void Build(List currentContracts) { throw new NotImplementedException(); } } 

如您所见,我定义了整个集合。

为什么?

我需要在表中为用户呈现数据,因为有几行属于完全/唯一用户(例如,20-30个商店项目被称为单个客户端)。

所以,我使用ADO.NET Entity从数据库获取数据。 在Controller对模型实例的绑定问题已经完成,我没有遇到任何问题,我只处理渲染问题。

我认为,它可以与@for东西一起使用,但不知道,如何使用特别是我的自定义模型会更好。

那么,如何使用我的模型在View渲染数据?

谢谢!

请参阅下面的视图。 您只需预览您的collections并显示合同即可。

控制器:

 public class ContactsController : Controller { public ActionResult Index() { var model = // your model return View(model); } } 

视图:

  <% foreach (var item in Model) { %>  <% } %> 
Foo
<%: item.Foo %>

剃刀:

 @model IEnumerable  @foreach (var item in Model) {  @} 
Foo
<@item.Foo>

如果您的操作返回合同List ,则可以在视图中执行以下操作:

 @model IEnumerable @foreach(ContractPlain contract in Model) { 
  • @contract.ContractGuid
  • @contract.SenderId
  • @contract.ContractStatus
  • @contract.CreditEnd
}