Reputation: 39
This is my model class
[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; }
and in my controller's index page i just pass a value through 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);
}
and this is my index view
<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()
)
when i rum this code and click on a link i got a internal server 500 error on index view that is saying that nullable object must have a value.
Upvotes: 0
Views: 7516
Reputation: 33857
Your problem is in here somewhere:
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
});
In all the places where you are doing a .Value
, you probably need to check that there is a value first, or use the null coalescing operator, e.g.:
...
Length = x.Length ?? 0,
...
Upvotes: 6