Reputation: 484
I Can't get the Display -> Order attribute to work.
I'm using VS2013 update 4 and the System.ComponentModel.DataAnnotations appears to be up to date with runtime version 4.0.30319.
I built a tiny MVC app with a small model:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace TestDataAnnotation.Models
{
[MetadataType(typeof(TestDB))]
public class TestDB
{
public int TestDBID { get; set; }
[Display(Order=2)]
public int MyProperty1 { get; set; }
[Display(Order=1)]
public int MyProperty2 { get; set; }
}
}
I created a controller and view using scaffolding. The GET for the Create is standard MVC
// GET: TestDBs/Create
public ActionResult Create()
{
return View();
}
The View is also standard MVC:
<div class="form-horizontal">
<h4>TestDB</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.MyProperty1, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MyProperty1, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MyProperty1, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.MyProperty2, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.MyProperty2, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.MyProperty2, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
MyProperty2 should display first, but it does not. I've tried numerous variations of this but can't get "Order" to work. I've had the MetadataType statement in the code and not in the code. I've tried order= -9. I've tried various different values for the order attribute. Display->Name works fine. Apparently, I'm missing some key idea here. Thanks for any and all help!
Upvotes: 0
Views: 1463
Reputation: 239430
The Order
parameter applies when there's something dynamic operating on the model and generating HTML, like a grid view. Here, you've literally rendered the properties in a given order. Setting Order
isn't going to override explicit HTML.
Upvotes: 1