C# 如何在 ASP.NET MVC 4 Razor 项目的视图中显示集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17368034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:19:48  来源:igfitidea点击:

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

c#asp.net-mvc-4razorrenderado.net-entity-data-model

提问by

I have the following Model:

我有以下型号:

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();
    }
}

As you can see, I defined a whole collection.

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

Why?

为什么?

I need to render the data in table for user, because there are several rows which belongs to the exact/unique user (e.g. 20-30 shop items are referred to the single client).

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

So, I'm getting data from the DB using ADO.NET Entity. The binding question to the model instance in Controlleris done and I don't have issues with that, I do with the rendering question only.

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

I think, it could be used with the @forstuff, but didn't know, how it would be better using especially my custom model.

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

So, how can I render the data in Viewusing my model?

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

Thanks!

谢谢!

采纳答案by Sam Leach

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

请参阅下面的视图。您只需对您的集合进行 foreach 并显示合同。

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>

回答by Samuel Parkinson

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

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

@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>
}