在跟随书籍示例时,ASP.NET MVC InvalidOperationException未被用户代码处理

时间:2016-08-08 12:09:23

标签: c# asp.net asp.net-mvc

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搜索,但我自己无法解决这个问题。

提前致谢。

0 个答案:

没有答案