namespace SportsStore.Domain.Entities
{
public class Product
{
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set; }
//[Required(ErrorMessage = "Please enter a product name")]
public string Name { get; set; }
[DataType(DataType.MultilineText)]
//[Required(ErrorMessage ="Please enter a description")]
public string Description { get; set; }
//[Required]
//[Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
public decimal Price { get; set; }
//[Required(ErrorMessage = "Please specify a category")]
public string Category { get; set; }
}
}
这是实体定义,下面是来自控制器的动作。
public ViewResult List(string category, int page = 1)
{
ProductsListViewModel model = new ProductsListViewModel
{
Products = repository.Products.Where(p => category == null || p.Category == category).OrderBy(p => p.ProductID).Skip((page - 1) * PageSize).Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ? repository.Products.Count() : repository.Products.Where(e => e.Category == category).Count()
},
CurrentCategory = category
};
return View(model);
}
当我在Product类中取消注释[Required ...]时,我从Visual Studio得到InvalidOperationException。但它可以正常使用(评论)。
我试图修复它并用Google搜索,但我自己无法解决这个问题。
提前致谢。