using DisplayTextFor with IEnumerable model

Topics: Writing modules
Apr 13, 2012 at 7:55 PM

this is more of a general MVC question, but I prefer the help from this community over all others because you guys rock!

I'm passing in an IEnumerable<model> into my View and I defined at the top of my View

@model IEnumerable<Namespace.Models.ModelName>

later on in my View, I have a table where I define header rows and then a foreach loop for the data rows that enumerates over the model I passed in.

My question is, can I use @Html.DisplayTextFor() in the table header rows to display the name that I've defined in the model?

I can't seem to figure out the syntax or how to do it with an IEnumerable. If I were passing in a single instance of the model, it works fine, but I can't seem to figure it out with the IEnumerable.

Thanks for any assistance.

Developer
Apr 13, 2012 at 9:10 PM

Well, since I feel called to account, I might as well try to answer that ;)

You should be able to use it like this:

@Html.DisplayTextFor(m => m[i].SomePropertyToDisplay)

To make this work you would need to either loop using a for construct, or use the foreach construct and introduce an index variable (i).

Developer
Apr 13, 2012 at 9:14 PM

I forgot to mention that since an IEnumerable<T> doesn't have an indexer, you would first need to project it into a list or an array:

var myList = Model.ToArray();

Then iterate over myList instead of Model.

Also update this: @Html.DisplayTextFor(m => myList[i].SomePropertyToDisplay)

Apr 13, 2012 at 10:59 PM

Thanks for the help...


My first obstacle is that these would be for the table header row, so
I wouldn't want to include them in the foreach loop or they'd display
for each row in the table.

I like your idea about trying to use an index though. I can send the
model in to the view as a List...there's no reason it has to be an
ienumerable. I don't know if

@Html.DisplayTextFor(m => myList[i].SomePropertyToDisplay)

will work outside of the foreach loop though...even with an index
value. I'll try it out.

Thanks again

Apr 13, 2012 at 11:25 PM

Well...after changing it to a list, using this:

@Html.DisplayTextFor(m => m[0].SomeProperty)

actually renders, but it gives me the value of the first SomeProperty in the list, not the DataAnnotation I had defined in the Model with DisplayName,

Developer
Apr 13, 2012 at 11:30 PM

You mean you decorated SomeProperty with the [DisplayName("Some Title")] attribute, but "Some Title" is not being rendered? Perhaps you could try the [Display] attribute instead?

Apr 13, 2012 at 11:34 PM

Do you mean [Display(Name="Some Title")]?

I tried that and got the same result. Using @Html.LabelFor(m => m[0].SomeProperty) works, but of course I get Some Title surrounded by a <label> tag

Developer
Apr 13, 2012 at 11:37 PM

Yes, that's what I meant.

That's odd. So it works when you simply use a single object as the model, but not when you use a list as the model?

Apr 13, 2012 at 11:44 PM

Actually, with a single object, using @Html.DisplayTextFor(m => m.SomeProperty) renders the value of the object as well. Only @Html.LabelFor actually renders the DisplayName.

Maybe I'm totally misunderstanding the DataAnnotation for DisplayName. I thought it could be used for other things other than a label. Is that what your impression was as well?

Apr 14, 2012 at 12:02 AM

I did some poking around and I can keep as an IEnumerable and access it as such

@Html.LabelFor(m => m.First().BenefitCode)

Of course this still surrounds it with a <label> tag which is what I wanted to avoid

Developer
Apr 14, 2012 at 12:04 AM

Actually, I have never used @Html.DisplayTextFor and didn't even know about it until this thread came up :)
This SO discussion talks about a [DisplayFormat] attribute. Perhaps try that one?

Apr 14, 2012 at 12:16 AM

That link will help with something else I want to change, so thanks.

I found the answer with a small helper class...

http://stackoverflow.com/questions/4947854/how-to-get-the-column-titles-from-the-displayname-dataannotation-for-a-strong

This seems to work great.

Thanks again for your assistance!

Developer
Apr 14, 2012 at 7:55 AM

Awesome!