'DisplayAttribute.Order does not change the order of fields
I used scaffolding to generate a view for the model below, in Visual Studio 2013 (.NET 4.5). I added the Display annotations afterwards. Why doesn't the LastName field appears above the FirstName field?
public class Order
{
[Display(Order=15001)]
public string FirstName { get; set; }
[Display(Order=15000)]
public string LastName { get; set; }
}
Does the DisplayAttribute.Order documentation say it only works on columns?
I'm reading Professional ASP.NET MVC 5 by Jon Galloway et al., that talks about doing this.
EDIT: the view (Create.cshtml). The original has more fields but I've omitted them like in the model:
@model MvcMusicStore.Models.Order
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", 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>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
