In some cases, when the properties are more than usual, it is painful to copy and drive some code after another to show all the properties of the model. Therefore, I want to know if there is a way to show all the properties of the Model dynamically. for example, we have this TestModel:
TestModel.cs
[Display(Name = "نام")]
[Required]
public string Name { get; set; }
[Display(Name = "ایمیل")]
[Required]
public string Email { get; set; }
[Display(Name = "شماره تماس")]
[Required]
public string PhoneNumber { get; set; }
Now I want to show both the DisplayName and the Value of this model in a razor, for example sth, like this:
TestRazor.cshtml
@foreach (var Item in Model.GetType().GetProperties())
{
<div class="row">
<p class="label">@Item.DisplayName</p>
<p class="value">@Item.Value</p>
</div>
<br />
<br />
}
source
share