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