0

嗨,我正在尝试将我的元数据添加到单独的文件中,以保持我的模型尽可能干净,但似乎有些问题,因为现在似乎某些属性不显示验证。这是我的模型类:

public partial class BookModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Author { get; set; }
    public string Description { get; set;}
    public DateTime PublicationDate { get; set; }
    public int CategoryId { get; set; }
    public decimal Price { get; set; }
    public string BookUrl { get; set; }
}

这是我的元数据部分类:

[MetadataType(typeof(BookModel))]
public partial class BookModelMetada
{
    public int Id { get; set; }

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

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

    [Required]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
    public DateTime PublicationDate { get; set; }

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

    [Required]
    [DataType(DataType.Currency)]
    [DisplayFormat(DataFormatString = "{0:c}")]
    public decimal Price { get; set; }

    public string BookUrl { get; set; }
}

奇怪的字段是 PublicationDate 和 Price 显示验证错误,但其他属性不显示。

我究竟做错了什么?

编辑:

查看代码:

 <p>
            @Html.LabelFor(model => model.Book.Name, "Book Name")
            @Html.TextBoxFor(model => model.Book.Name)
        </p> 
        <p>
            @Html.LabelFor(model => model.Book.Author)
            @Html.TextBoxFor(model => model.Book.Author)
        </p>
        <p>
            @Html.LabelFor(model => model.Book.PublicationDate ,"Publication Date")
            @Html.TextBoxFor(model => model.Book.PublicationDate, new { @class="datepicker" })
        </p>
        <p>
            @Html.LabelFor(model => model.Book.Price)
            @Html.TextBoxFor(model => model.Book.Price)
        </p>
        <p>
            @Html.LabelFor(model => model.Book.CategoryId, "Select category")
            @Html.DropDownListFor(model => model.Book.CategoryId, new SelectList(Model.Categories, "Id", "Name"))
        </p>
        <p> 
            @Html.LabelFor(model => model.Book.Description)
            @Html.TextAreaFor(model => model.Book.Description)
        </p>
        <p>
            <input type="submit" value="Create" class="link"/>
            @Html.ActionLink("Back to List", "Books", "ProductManager", null, new { @class = "link" })
        </p>
4

2 回答 2

2

问题是我添加[MetadataType(typeof(BookModel))]了元数据类而不是模型类。

于 2013-03-22T12:03:32.413 回答
0

可能有一个或多个原因:但首先要确保在视图或视图布局中包含 jquery.unobtrusive 和 jquery.validate。但是,您的视图似乎不清楚,您必须使用@Html.LabelFor(model =>model.Book.Author). 请发布您的完整视图。

于 2013-03-22T09:37:19.243 回答