EditorForModel displays inherited properties last

53 views Asked by At

I'm writing an ASP.NET MVC web app that includes a parts list. Because of this I have created a class hierarchy. The problem I have is that using Html.EditorForModel() displays the properties from the inherited base class last when I want them to be displayed first.

Here's an example of what I'm doing:

Part.cs

public class Part
{
    [Key]
    [HiddenInput(DiplayValue = false)]
    public Nullable<int> ID { get; set; }

    [Required]
    public int Price { get; set; }

    [Required]
    public string Brand { get; set; }
}

Screw.cs

public class Screw : Part
{
    public int Length { get; set; }
    public int Width { get; set; }
}

Then when I call Html.EditorForModel in my View this is the order the fields show up in:

  • Length
  • Width
  • Price
  • Brand

But I want it to show up in this order:

  • Price
  • Brand
  • Length
  • Width

I've tried specifying the [Column(Order = #)] attribute but that didn't change anything. Outside of creating an EditorTemplate for each Part, how would I go about doing this?

1

There are 1 answers

0
Ilya Chumakov On BEST ANSWER

Use Display attribute to set order:

[Display(Order = 2)]
public int Id { get; set; }

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