可以为空的对象必须具有值

时间:2012-07-02 14:51:20

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

这是我的模特课

   [HiddenInput(DisplayValue = false)]
    public long ProductId { get; set; }

    [Display(Name="Alarm Point")]
    public short AlarmPoint { get; set; }

    [Required]
    public long? MeasurementId { get; set; }

    [Display(Name="Measurement Id")]
    public IList<SelectListItem> Measurement { get; set; }


    [Display(Name="Length")]
    public decimal Length { get; set; }


    [Display(Name="Breadth")]
    public decimal Breadth { get; set; }


    [Display(Name="Height")]
    public decimal Height { get; set; }


    [Display(Name="Weight")]
    public decimal Weight { get; set; }


    [Display(Name="Is Active")]
    public bool IsActive { get; set; }


    [Display(Name="Is Sellable")]
    public bool IsSellable { get; set; }

并且在我的控制器的索引页面中,我只是通过Json传递一个值。

   [GridAction]
    public ActionResult ProductList()
    {
        var collection = _productService.GetAllProduct().Select(x =>
            new ProductModel()
            {
                ProductId = x.ProductId,
                Title = x.Title,
                CategoryId = x.CategoryId,
                Description = x.Description,
                Barcode = x.Barcode,
                AlarmPoint = x.AlarmPoint.Value,
                MeasurementId = x.MeasurementId,
                Length = x.Length.Value,
                Breadth = x.Breadth.Value,
                Height = x.Height.Value,
                Weight = x.Weight.Value,
                IsActive = x.IsActive.Value,
                IsSellable = x.IsSellable.Value,
                IsPurchasable = x.IsPurchasable.Value
            });
        return Json(collection, JsonRequestBehavior.AllowGet);
    }

这是我的索引视图

          <h1 style="font-size: 25px ">View</h1>
           <br />
            @(Html.Telerik().Grid<CommerceSuite.Web.Models.Product.ProductModel>()
                           .Name("Product")
           .DataBinding(databinding =>
           {
               databinding.Ajax().Select("ProductList", "Product");
           })
           .Columns(columns =>
           {
               columns.Bound(p => p.ProductId)
               .ClientTemplate("<input type='checkbox' name='checkedRecords' value='<#= ProductId #>' />")
               .Title("")
               .Width(20)
               .HtmlAttributes(new { style = "text-align:center" });
               columns.Bound(p => p.Title).Width(200);
               columns.Bound(p => p.ProductId).Width(200);
               columns.Bound(p => p.CategoryId).Width(200);
               columns.Bound(p => p.Barcode).Width(200);
               columns.Bound(p => p.ProductId)
               .ClientTemplate("<input type='button' name='<#=ProductId #>' onclick='csPopupOpen(\"Edit BillOf Material\",\"" + @Url.Action("Edit") + "\",<#=ProductId #>,\"" + @Url.Action("Index") + "\")' value='Edit' > <input type='button' name='<#=ProductId #>' onclick='csPopupOpen(\"Delete BillOf Material\",\"" + @Url.Action("Delete") + "\",<#=ProductId #>)' value='Delete' > ").Width(210);
           })
           .Pageable()
           .Scrollable()
           .Sortable()
     )

当我朗读此代码并点击链接时,我在索引视图上遇到内部服务器500错误,该错误表明可以为空的对象必须具有值。

1 个答案:

答案 0 :(得分:6)

你的问题出现在这里:

var collection = _productService.GetAllProduct().Select(x =>
            new ProductModel()
            {
                ProductId = x.ProductId,
                Title = x.Title,
                CategoryId = x.CategoryId,
                Description = x.Description,
                Barcode = x.Barcode,
                AlarmPoint = x.AlarmPoint.Value,
                MeasurementId = x.MeasurementId,
                Length = x.Length.Value,
                Breadth = x.Breadth.Value,
                Height = x.Height.Value,
                Weight = x.Weight.Value,
                IsActive = x.IsActive.Value,
                IsSellable = x.IsSellable.Value,
                IsPurchasable = x.IsPurchasable.Value
            });

在您正在进行.Value的所有地方,您可能需要先检查是否存在值,或使用空合并运算符,例如:

...
Length = x.Length ?? 0,
...