2012-07-02 116 views
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錯誤上是說,可空對象必須有一個值。

+0

在你的LINQ你已經錯過了。價值我認爲MeasurementId = x.MeasurementId, –

+0

@ ebad86 - 如果兩者都爲空的,然後添加.value的是潛在的問題。 – Paddy

回答

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, 
... 
+4

可空類型也有'.GetValueOrDefault()',您可以使用它來代替空合併運算符。 –

+0

感謝帕迪的工作。 –

相關問題