ASP.NET MVC Iterate over Model Properties with Data Annotations

Description of the problem

My problem is similar to this question , but instead of applying annotations to the property name through reflection (processed by ModelMetadata.DisplayName) I apply them to the value (not processed by ModelMetadata).

Detailed description

In the context of an ASP.NET MVC program.
Suppose I have a Model class

public class Model
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string NickName { get; set; }
    public string Address { get; set; }
    public int Phone { get; set; }

    [Display(Name = "Start Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime StartDate { get; set }

    [Display(Name = "End Date")]
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime EndDate { get; set }
}

Then suppose that this Model is used in at least 5 different representations, where the value of each property should be fully displayed. (Sometimes for each instance, sometimes for several, sometimes for one).

I can manually list each access to the properties inside it. <td>@Html.DisplayFor(item.<Property>)</td>
for each species.

, Model , (, , ). .

PropertyInfo , <td>@property.GetValue(item)</td>

DisplayFor(x) , x=>property.GetValue(item), , , DateTime
01/01/1990
01-Jan-90 12:00:00 AM
, , , .

( ) :

  • [Failed] , @property.GetValue(item)
    []
  • [Failed] DisplayFor MethodInfo, Access Accessor DisplayFor(x => property.GetGetMethod()), .Invoke x.
    [/Edit]
  • , ,
    • , ​​ , ,
    • DisplayFor " " DisplayFor,
  • Model (SortedList?) "Prop", "Prop" - , Name Value.

@Html.DisplayFor(m=>property.GetValue(item)
@Html.DisplayFor(m=>item.Properties[i].Value)
, "" ( [ "" ]) (.Value), .

[]
Utility, DisplayFormatAttribute PropertyInfo DisplayFormatString, "{0}", . ViewModel. , , " ", . [/Edit]

, ...
, , ? ?
, , , , , ?

+4
2

, - :

@foreach (var property in Model.GetType().GetProperties())
{
    <li>@property.GetValue(Model, null)</li>
}
0

, , , , Microsoft !

Microsoft ( , ), DisplayFor.
, - , , , , Model .

:

@foreach (var thing in Model.CollectionOfThings)
{
    <tr>
        @foreach (var prop in typeof(Thing).GetProperties())
        {
            <td>
                @{
                    Html.RenderPartial("~/Views/Shared/_DisplayForReflectedProperty.cshtml", 
                    new Tuple<Thing, PropertyInfo>(thing, prop));
                }
            </td>
        }
}

_DisplayForReflectedProperty ,

@using WebApplication1.Models
@using System.Reflection
@using WebApplication1.Extensions

@model Tuple<Thing, PropertyInfo>

@Html.DisplayFor("Item1."+Model.Item2.Name)

DisplayFor , , ( EditorFor to DisplayFor):

var lambda = System.Linq.Dynamic.DynamicExpression.ParseLambda(typeof(TModel), 
             null, expression);

, ( , -) , , , ""!

0

Source: https://habr.com/ru/post/1676696/


All Articles