C# – 如何在ViewView中显示集合MVC 4 Razor项目?
发布时间:2020-12-15 17:36:31 所属栏目:百科 来源:网络整理
导读:我有以下型号: 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 ContractSta
我有以下型号:
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<ContractPlain> listOutput; public void Build(List<ContractPlain> listInput) { listOutput = new List<ContractPlain>(); } public List<ContractPlain> GetContracts() { return listOutput; } internal void Build(List<contract> currentContracts) { throw new NotImplementedException(); } } 你可以看到,我定义了一个整个集合. 为什么? 我需要为用户呈现表中的数据,因为有几行属于精确/唯一用户(例如,20-30个商店项目被称为单个客户端). 所以,我使用ADO.NET实体从数据库获取数据.控制器中的模型实例的绑定问题已经完成,我没有问题,我只用渲染问题. 我认为,它可以与@for的东西一起使用,但不知道,特别是我的自定义模型会更好. 那么,如何使用我的模型在View中呈现数据? 谢谢! 解决方法
请参阅下面的视图.您只需在您的收藏品上展示并显示合同.
控制器: public class ContactsController : Controller { public ActionResult Index() { var model = // your model return View(model); } } 视图: <table class="grid"> <tr> <th>Foo</th> </tr> <% foreach (var item in Model) { %> <tr> <td class="left"><%: item.Foo %></td> </tr> <% } %> </table> 剃刀: @model IEnumerable<ContractPlain> <table class="grid"> <tr> <th>Foo</th> </tr> @foreach (var item in Model) { <tr> <td class="left"><@item.Foo></td> </tr> @} </table> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |