How to display a collection in View of ASP.NET MVC 4 Razor project?

前端 未结 2 1851
南笙
南笙 2020-12-17 19:07

I have the following Model:

public class ContractPlain
{
    public int Id { get; set; }
    public Guid ContractGuid { get; set; }
    public int SenderId {         


        
相关标签:
2条回答
  • 2020-12-17 19:43

    See the view below. You simply foreach over your collection and display the Contracts.

    Controller:

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

    View:

    <table class="grid">
    <tr>
        <th>Foo</th>
    </tr> 
    
    <% foreach (var item in Model) { %>
    
    <tr>
        <td class="left"><%: item.Foo %></td>
    </tr>
    
    <% } %>
    
    </table>
    

    Razor:

    @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>
    
    0 讨论(0)
  • 2020-12-17 19:49

    If your action returns a List of contracts you can do the following in the view:

    @model IEnumerable<ContractPlain>
    
    @foreach(ContractPlain contract in Model) 
    {
        <ul>
            <li>@contract.ContractGuid</li>
            <li>@contract.SenderId</li>
            <li>@contract.ContractStatus</li>
            <li>@contract.CreditEnd</li>
        </ul>
    }
    
    0 讨论(0)
提交回复
热议问题