C# – 如何在ViewView中显示集合MVC 4 Razor项目?

前端之家收集整理的这篇文章主要介绍了C# – 如何在ViewView中显示集合MVC 4 Razor项目?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下型号:
  1. public class ContractPlain
  2. {
  3. public int Id { get; set; }
  4. public Guid ContractGuid { get; set; }
  5. public int SenderId { get; set; }
  6. public int RecvId { get; set; }
  7. public int ContractType { get; set; }
  8. public string ContractStatus { get; set; }
  9. public DateTime CreatedTime { get; set; }
  10. public DateTime CreditEnd { get; set; }
  11. }
  12.  
  13. public class Contrtacts
  14. {
  15. List<ContractPlain> listOutput;
  16.  
  17. public void Build(List<ContractPlain> listInput)
  18. {
  19. listOutput = new List<ContractPlain>();
  20. }
  21.  
  22. public List<ContractPlain> GetContracts()
  23. {
  24. return listOutput;
  25. }
  26.  
  27. internal void Build(List<contract> currentContracts)
  28. {
  29. throw new NotImplementedException();
  30. }
  31. }

你可以看到,我定义了一个整个集合.

为什么?

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

所以,我使用ADO.NET实体从数据库获取数据.控制器中的模型实例的绑定问题已经完成,我没有问题,我只用渲染问题.

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

那么,如何使用我的模型在View中呈现数据?

谢谢!

解决方法

请参阅下面的视图.您只需在您的收藏品上展示并显示合同.

控制器:

  1. public class ContactsController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. var model = // your model
  6.  
  7. return View(model);
  8. }
  9. }

视图:

  1. <table class="grid">
  2. <tr>
  3. <th>Foo</th>
  4. </tr>
  5.  
  6. <% foreach (var item in Model) { %>
  7.  
  8. <tr>
  9. <td class="left"><%: item.Foo %></td>
  10. </tr>
  11.  
  12. <% } %>
  13.  
  14. </table>

剃刀:

  1. @model IEnumerable<ContractPlain>
  2.  
  3. <table class="grid">
  4. <tr>
  5. <th>Foo</th>
  6. </tr>
  7.  
  8. @foreach (var item in Model) {
  9.  
  10. <tr>
  11. <td class="left"><@item.Foo></td>
  12. </tr>
  13.  
  14. @}
  15.  
  16. </table>

猜你在找的C#相关文章