Meca
Meca

Reputation: 148

Ordering fields in ASP.NET Razor pages when using @Html.EditorForModel()

I have a razor view with the following:

    @Html.EditorForModel()

It displays the properties in the model in the order they appear in the model. Is it possible to order them using some kind of annotation that is already built into the MVC framework (ASP.NET MVC 5.2.6)?

Upvotes: 0

Views: 195

Answers (1)

Andrew
Andrew

Reputation: 258

Try setting an order on your display attributes within your model for each property, like so:

public class Person
{
    [Display(Name = "First Name", Order = 0)]  
    public string FirstName { get; set; }  

    [Display(Name = "Last Name", Order = 1)]  
    public string LastName { get; set; }
} 

More information about the DisplayAttribute.Order property can be found here:

https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.displayattribute.order?redirectedfrom=MSDN&view=netframework-4.7.2#System_ComponentModel_DataAnnotations_DisplayAttribute_Order

Upvotes: 1

Related Questions