I have the following view-model for my MVC3 Razor view:
public class UserProfileModel
{
public Person[] Persons { get; set; }
//some other fields
}
I want to display all the persons in my Razor view like:
foreach (var person in Model.Persons)
{
<div>
@* some custom formatting *@
@Html.Display(person)
</div>
}
@Html.Display
or @Html.DisplayFor
seems to not work for me..
I can create a separate stongly-typed view using Person
as a model and call @Html.DisplayForModel
there, but is there a way to go without a separate view?
Create a partial view file called Person.cshtml
inside ~/Views/Shared/DisplayTemplates
. Make it strongly typed to Person
class.
Implement the structure of your view.
Then when you call it like below (on your case), you will get what you expected :
foreach (var person in Model.Persons)
{
<div>
@* some custom formatting *@
@Html.DisplayFor(m => person)
</div>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With