This project is read-only.

using DisplayTextFor with IEnumerable model

Topics: Writing modules
Apr 13, 2012 at 8: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.

Apr 13, 2012 at 10: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).

Apr 13, 2012 at 10: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 11: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 14, 2012 at 12:25 AM

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,

Apr 14, 2012 at 12:30 AM

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 14, 2012 at 12:34 AM

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

Apr 14, 2012 at 12:37 AM

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 14, 2012 at 12:44 AM

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 1: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

Apr 14, 2012 at 1: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 1:16 AM

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

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

This seems to work great.

Thanks again for your assistance!

Apr 14, 2012 at 8:55 AM