0

这是我的模型课

   [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 错误,它说可以为空的对象必须有一个值。

4

1 回答 1

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
            });

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

...
Length = x.Length ?? 0,
...
于 2012-07-02T14:59:02.017 回答