如何显示ASP.NET MVC 4剃刀项目视图的集合? [英] How to display a collection in View of ASP.NET MVC 4 Razor project?

查看:154
本文介绍了如何显示ASP.NET MVC 4剃刀项目视图的集合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

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.

为什么?

我需要渲染表为用户的数据,因为有几行属于确切/唯一的用户(例如20-30本店物品都提到了单一客户)。

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).

所以,我从DB使用ADO.NET实体获取数据。在控制器的模型实例绑定问题做,我没有问题,与我做只渲染问题。

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

我想,这可能与 @for 的东西可以使用,但不知道,怎么会是更好的使用尤其是我的自定义模型。

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

那么,怎样才能用我的模式我渲染查看中的数据?

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

谢谢!

推荐答案

请参见下面的视图。您只需在的foreach您的收藏和展示的合同。

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

控制器:

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>

这篇关于如何显示ASP.NET MVC 4剃刀项目视图的集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆