是否有可能在索引视图使用LabelFor为标题行 [英] Is it possible to use LabelFor for header row in Index view

查看:126
本文介绍了是否有可能在索引视图使用LabelFor为标题行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ASP.NET MVC索引视图利用DataAnnotation值。有趣的是,code发生器使用的字段名称,而不是 Html.LabelFor(M = GT; Model.ColumNames [的博文])(例如,博文。)或类似的东西(我做了它)。

I am trying to leverage DataAnnotation values in ASP.NET MVC Index view. Interesting, code generator uses field names (eg., BlogPost) as opposed to Html.LabelFor(m => Model.ColumNames["BlogPost"]) or something similar (I made it up).

这是否意味着,如果模型的IEnumerable(如索引视图),那么它是不可能得到由DataAnnotation指定的显示名称?很难相信...

Does it mean, that if the Model is IEnumerable (as in Index view), then it is not possible to get to the Display Name that is specified by DataAnnotation? Hard to believe...

我的code是当前MVC 2和MVC 3的组合,如果这有什么差别。

My code is currently a mix of MVC 2 and MVC 3, if it makes any difference.

推荐答案

您应该,最起码,能得到你想要的东西用局部视图(ASCX对的WebForms)或显示/编辑模板。在可以循环在你的索引页的枚举,并通过该项目变成一个局部视图或模板。

You should, at the very least, be able to get what you want by using a partial view (ascx for WebForms) or a Display/Editor template. In your index page you can loop over your enumerable and pass the item into either a partial view or a template.

有可能是一个办法做到这一点,而不必做,因为我已经建议(我会希望看到的答案),但是我建议应该可以正常工作。

There may be a way to do it without having do as I've suggested (and I would be interested in seeing the answer), but what I've suggested should work fine.

编辑:

一些澄清后,这是我更新的答案。

After some clarification, here is my updated answer.

您仍然可以得到一个属性标签,同时仍尊重 DisplayAttribute 数据注解。我想这真正的快,这似乎很好地工作。

You can still get the label for a property while still respecting the DisplayAttribute in data annotations. I tried this real quick and it seems to work fine.

在我看来,我有以下几点:

In my view I have the following:

Html.LabelFor(m => m.BlogPosts.First().BlogPostTitle)

如果有枚举本身没有项目这个工作甚。当我第一次测试,这给我的属性名,然后我加饰有属性的 DisplayAttribute 并显示,而不是标准属性名称的名称属性的值。

This worked even if there were no items in the enumeration itself. When I first tested this I got the property name, then I added decorated the property with the DisplayAttribute and the value of the name property was displayed instead of the standard property name.

这篇关于是否有可能在索引视图使用LabelFor为标题行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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