我如何做一个一个ASP.NET MVC3 DisplayTemplate它显示一个集合的内容? [英] How an i make an ASP.NET MVC3 DisplayTemplate which displays the contents of a Collection?

查看:173
本文介绍了我如何做一个一个ASP.NET MVC3 DisplayTemplate它显示一个集合的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示集合的内容,在我看来..但使用 DisplayTemplate 来处理视图的定义,对于特殊的属性/对象。

如:

 < D​​IV CLASS =显示标签>&FOOS LT; / DIV>
< D​​IV CLASS =显示字段> @ Html.DisplayTextFor(_ => Model.Foos)LT; / DIV>

和foo的对象。

 公共类Foo
{
    公共字符串名称{;组; }
    公共字符串胡说{搞定;组; }
}

和...

 公共字符串为MyModel
{
    公众的ICollection<富> FOOS {搞定;组;}
}

所以,我创建了一个文件夹,名为 DisplayTemplates ,在这个控制器我查看文件夹。
然后,我在一个名为添加 Foo.cshtml ,内容如下

  @model了myNameSpace富.....@Model [@ Model.Blah] // @ Model.Name

和我所得到显示在我的看法?


  

    

System.Collections.Generic.List`1 [myNameSpace对象.....美孚]


  

。我已经确认,在此集合中至少有一个项目。
任何想法,伙计?


解决方案

找到我的答案:)

我(有错误)

  @ Html.DisplayTextFor(_ => Model.Foos)

但我不应该一直在使用 DisplayTextFor ,但 DisplayFor

  @ Html.DisplayFor(X => x.Model.Foos)

史蒂夫·桑德森临ASP.NET MVC2框架(第2版) .pdf和423页上,他说(我诚恳希望我没有侵犯版权,在这里)。


  

例如,你现在可以渲染
  人的枚举集合
  有观点一行实例
  标记 - 例如:

 <%:Html.DisplayFor(X => x.MyPersonCollection)%GT;
//这将会使Person.ascx部分一次
//对于集合中的每个项目。


和他是正确的,这个文本突出我的错误。

WIN:)

I'm trying to display the contents of a collection, in my View .. but using a DisplayTemplate to handle the definition of the view, for that specialized property/object.

eg.

<div class="display-label">Foos</div>
<div class="display-field">@Html.DisplayTextFor(_ => Model.Foos)</div>

and the foo object is..

public class Foo
{
    public string Name { get; set; }
    public string Blah { get; set; }
}

and...

public string MyModel
{
    public ICollection<Foo> Foos { get; set;}
}

So i created a folder called DisplayTemplates, in my View folder for this Controller. I then added in a file called Foo.cshtml with the following content

@model MyNamespace.....Foo

@Model [@Model.Blah] @Model.Name

and what i have getting displayed on my view?

System.Collections.Generic.List`1[MyNamespace.....Foo]

. I've confirmed that there is at least one item in this collection. Any ideas, folks?

解决方案

Found my answer :)

I (incorrectly had)

@Html.DisplayTextFor(_ => Model.Foos)

but I should NOT have been using DisplayTextFor but DisplayFor

@Html.DisplayFor(x => x.Model.Foos)

Also, I have purchased copy of Steve Sanderson's Pro ASP.NET MVC2 Framework (2nd Edition).pdf and on page 423 he says (and I sincerly hope I'm not infringing on copyright, here).

For example, you could now render an enumerable collection of Person instances with a single line of view markup—for example:

<%:Html.DisplayFor(x => x.MyPersonCollection) %>
//This would render the Person.ascx partial once 
//for each item in the collection. 

And he was correct and this text highlighted my mistake.

Win :)

这篇关于我如何做一个一个ASP.NET MVC3 DisplayTemplate它显示一个集合的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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