razor - Get scaffolder to generate fields in specific order -


i trying asp.net core mvc scaffold razor view fields in different order apparently default alphabetical order. i've got simple model:

public class application : entitybase {     [display(name = "naam", order = 1)]     public string name { get; set; }      [display(name = "omschrijving", order = 2)]     public string description { get; set; } } 

i want scaffolder generate field name before description. how this?

i've been trying come solution in razor template. relevant code is:

... ienumerable<propertymetadata> properties = model.modelmetadata.properties; foreach (var property in properties) {     if (property.scaffold && !property.isprimarykey && !property.isforeignkey)     { ... 

i hoping property had order-property, write like

foreach (var property in properties.orderby(p => p.order)) 

any ideas?

so, after (deep) digging came solution. allready customized templates, acceptable add yet customization. ended creating helper class scaffoldhelpers.cs:

public class scaffoldhelpers {     public static ienumerable<propertymetadata> getpropertiesindisplayorder(string typename, ienumerable<propertymetadata> properties)     {         type type = type.gettype($"{typename}, {typename.split('.')[0]}");         sortedlist<string, propertymetadata> propertieslist = new sortedlist<string, propertymetadata>();         foreach (propertymetadata property in properties)         {             int order = 0;             if (type != null)             {                 var member = type.getmember(property.propertyname)[0];                 var displayattribute = member.getcustomattribute<system.componentmodel.dataannotations.displayattribute>();                 if (displayattribute != null)                 {                     order = displayattribute.order;                 }             }             propertieslist.add($"{order:000} - {property.propertyname}", property);         }         return propertieslist.values.asenumerable();     }  } 

this iterates on properties, , determines if [display()] attribute specified. if so, gets value of order-parameter. if don't specify this, order-property zero. using sortedlist , making sure key ordered specified order, i'm able return ienumerable<propertymetadata> in desired order.

in template, needed add @using helper-class. after that, insert following template:

... ienumerable<propertymetadata> properties = model.modelmetadata.properties;  // added: properties = scaffoldhelpers.getpropertiesindisplayorder(model.viewdatatypename, properties);  foreach (var property in properties) {     if (property.scaffold && !property.isprimarykey && !property.isforeignkey)     { ... 

that's it!


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -