views:

500

answers:

2

I set values for the Order property of the Display attribute in my model metadata.

[MetadataType(typeof(OccasionMetadata))]
public partial class Occasion
{
    private class OccasionMetadata
    {
        [ScaffoldColumn(false)]
        public object Id { get; set; }

        [Required]
        [Display(Name = "Title", Order = 0)]
        public object Designation { get; set; }

        [Required]
        [DataType(DataType.MultilineText)]
        [Display(Order = 3)]
        public object Summary { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        [Display(Order = 1)]
        public object Start { get; set; }

        [Required]
        [DataType(DataType.DateTime)]
        [Display(Order = 2)]
        public object Finish { get; set; }
    }
}

I present my models in strongly-typed views using the DisplayForModel and EditorForModel methods.

<%= Html.DisplayForModel() %>

and

<%= Html.EditorForModel() %>

But, ASP.NET MVC 2 displays the fields out of order! What might I have wrong?

+3  A: 

.NET 4 DataAnnotations comes with a new Display attribute that has several properties including specifying the value that is used for display in the UI and a ResourceType. Unfortunately, this attribute is new and is not supported in MVC 2 RTM.

The good news is it will be supported and is currently available in the MVC Futures release.

The steps to get this working are shown below...

from Localization in ASP.NET MVC 2 using ModelMetadata by Raj Kaimal

Raj Kaimal
Thanks for writing this, Raj. It's a big help.
Zack Peterson
Thanks for the edit, Zach :-)
Raj Kaimal
I've installed Visual Studio 2010 RTM and have tried to add the ASP.NET MVC 2 Futures to my project. The Display attribute still isn't working.
Zack Peterson
Zach, did you follow all the steps in my post?
Raj Kaimal
I just tried it without any issues.
Raj Kaimal
A: 

Brad Wilson said November 2009:

There is no support for order in MVC 2, and it's not likely to be there until MVC 3. One major reason is that DataAnnotations in .NET 4 have added ordering support, but since we rely on 3.5, we cannot do it yet.

from comment on "ASP.NET MVC 2 Templates, Part 5: Master Page Templates"

Zack Peterson
Using the model metadata provider in ASP.NET MVC 2 Futures allows Name to work, but not Order. http://stackoverflow.com/questions/2998865
Zack Peterson
Confirmed June 9, 2010 http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html#comment-6a00e54fbd8c4988340133f05a28b5970b
Zack Peterson