Reputation: 515
I have a view in my ASP.net MVC project which is associated to a controller (SubmissionController) and class (Submission).
In the view, I am displaying some values from the class using:
@Html.DisplayFor(Function(model) model.Created)
This works perfectly, but what I want to achieve now, is to be able to get the display name of a Collection nested within the model I reference above.
To make that clearer; I am viewing a single Submission of many Submissions in the view (using the Details function of the SubmissionController). Each submission has many Photos, and it is these photos I want to display in the Submission details view along with the submission details themselves.
To display the photos, I have the following piece of code creating a table of photos...
<tbody>
@For Each item In Model.Photos
Dim currentItem = item
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) currentItem.Photo_ID)
</td>
<td>
@Html.DisplayFor(Function(modelItem) currentItem.Photo_Status1.Value)
</td>
<td>
@Html.DisplayFor(Function(modelItem) currentItem.Photo_Size)
</td>
<td>
@Html.DisplayFor(Function(modelItem) currentItem.Photo_Taken)
</td>
</tr>
Next
</tbody>
Whilst untested, I believe the above code should work just fine.
With this in mind, How on earth do I get the DisplayNameFor values for Photo fields/properties?
This apparently, does not work!
@Html.DisplayNameFor(Function(model) model.Photos.Photo_Size)
By the way, I don't want to write in the header names manually, they must come from the model.
DigitalD's answer was almost there, here is the solution.
@Dim photoModel = New Photo()
@Html.DisplayNameFor(Function(photoModelx) photoModel.Photo_ID)
Upvotes: 0
Views: 1580
Reputation: 4310
@Html.DisplayNameFor(Function(model) model.Photos.Photo_Size)
won't work because model.Photos
is likely an IEnumerable
and doesn't have a Photo_Size property. You might get away with passing in an empty model item like so:
@Dim displayModel = new Photo()
@Html.DisplayNameFor(Function(displayModel) displayModel.Photo_Size)
etc.
Upvotes: 1
Reputation: 1662
If I am understanding correctly, you are trying to get the name of the field you are showing ("Photo Taken", "Photo Size", etc)? If this is the case, I would suggest using DataAnnotations and LabelFor(item=>item.Field). This will give you what you are looking for I believe.
Upvotes: 0