MVC 5 DataAnnotation订单属性

时间:2015-01-12 15:12:26

标签: asp.net-mvc-5

我无法获得展示广告 - >工作的订单属性。

我正在使用VS2013更新4,而System.ComponentModel.DataAnnotations似乎与运行时版本4.0.30319保持同步。

我用一个小型号构建了一个小型的MVC应用程序:



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; }
    }
}




我使用脚手架创建了一个控制器和视图。 Create的GET是标准MVC



        // GET: TestDBs/Create
        public ActionResult Create()
        {
            return View();
        }




View也是标准的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>
}
&#13;
&#13;
&#13;

MyProperty2应首先显示,但不会显示。我已经尝试了很多这方面的变化,但无法获得&#34; Order&#34;上班。我在代码中有MetadataType语句,而不是在代码中。我试过订单= -9。我为订单属性尝试了各种不同的值。显示 - >名称工作正常。显然,我在这里错过了一些关键的想法。感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

当模型上有动态操作并生成HTML时,Order参数适用,如网格视图。在这里,您实际上按给定顺序呈现了属性。设置Order不会覆盖显式HTML。