I am working on a page where I have a dropdownlist. Once the user makes a selection, the Client side Change event will load a Telerik Grid based off of the dropdownlist’s selection. The issue I’m encountering currently is when I try to load the page, I get the following error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 30: <tr>
Line 31: <td colspan="2">
Line 32: @(Html.Telerik().Grid<ProductModel>(Model.Products.Data)
Line 33: .Name("classification-products-grid")
Line 34: .Columns(columns =>
Below is all of my code. If anyone has any ideas, I’d appreciate it. Thanks.
Model (ClassificationListModel.cs)
public partial class ClassificationListModel : BaseNopModel
{
public ClassificationListModel()
{
Classifications = new List<ClassificationModel>();
}
[NopResourceDisplayName("Admin.Reports.Classifications.Classification")]
public int Classification { get; set; }
public List<ClassificationModel> Classifications { get; set; }
public GridModel<ProductModel> Products { get; set; }
}
public partial class ClassificationModel : BaseNopModel
{
public int Id { get; set; }
public string Name { get; set; }
}
View (Classifications.cshtml)
@model ClassificationListModel
@using Nop.Admin.Models.Reports;
@using Telerik.Web.Mvc.UI;
@{
var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
ViewBag.Title = @T("Admin.Reports.Classifications").Text;
}
@using (Html.BeginForm())
{
<div class="section-header">
<div class="title">
<img src="@Url.Content("~/Administration/Content/images/ico-catalog.png")" alt="" />
@T("Admin.Reports.Classifications")
</div>
</div>
<table width="100%">
<tr>
<td class="adminTitle">
@Html.NopLabelFor(model => model.Classification)
</td>
<td class="adminData">
@Html.DropDownListFor(model => model.Classification, new SelectList(Model.Classifications, "Id", "Name"), "Select a Classification")
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2">
@(Html.Telerik().Grid<ProductModel>(Model.Products.Data)
.Name("classification-products-grid")
.Columns(columns =>
{
columns.Bound(x => x.Id)
.ReadOnly(true)
.Visible(false);
columns.Bound(x => x.Name);
columns.Bound(x => x.Published)
.Width(100)
.Template(x => x.Published.ToString().ToLower())
.Centered();
})
.Pageable(settings => settings.Total(Model.Products.Total).PageSize(gridPageSize).Position(GridPagerPosition.Both))
.ClientEvents(events => events.OnDataBinding("onDataBinding"))
.DataBinding(dataBinding => dataBinding.Ajax().Select("ProductList", "Reports"))
.EnableCustomBinding(true))
</td>
</tr>
</table>
<script type="text/javascript">
$(document).ready(function () {
$("#Classification").change(function () {
$("#classification-products-grid").data("tGrid").rebind();
});
});
function onDataBinding(e) {
var specificationOptionId = $("#Classification").data("tDropDownList").value();
if (specificationOptionId != 0)
e.data = $.extend(e.data, {
specificationOptionId: $("#Classification").data("tDropDownList").value()
});
}
</script>
}
Controller (ReportsController.cs)
[AdminAuthorize]
public class ReportsController : BaseNopController
{
#region Fields
private readonly IPermissionService _permissionService;
private readonly IProductService _productService;
private readonly ISpecificationAttributeService _specificationAttributeService;
private readonly IWorkContext _workContext;
#endregion
#region Constructor
public ReportsController(IPermissionService permissionService, IProductService productService,
ISpecificationAttributeService specificationAttributeService, IWorkContext workContext)
{
this._permissionService = permissionService;
this._productService = productService;
this._specificationAttributeService = specificationAttributeService;
this._workContext = workContext;
}
#endregion
#region Utilities
[NonAction]
private List<ClassificationModel> PrepareClassificationListModel(IList<SpecificationAttributeOption> specificationAttributeOptions)
{
var returnData = new List<ClassificationModel>();
foreach (var specificationAttributeOption in specificationAttributeOptions)
{
var classification = new ClassificationModel
{
Id = specificationAttributeOption.Id,
Name = specificationAttributeOption.Name
};
returnData.Add(classification);
}
return returnData;
}
#endregion
#region Methods
#region Classifications
public ActionResult Index()
{
return RedirectToAction("Classifications");
}
public ActionResult Classifications()
{
var classificationListModel = new ClassificationListModel();
var specificationAttributeOptions = _specificationAttributeService.GetSpecificationAttributeOptionsBySpecificationAttribute(5);
var classifications = PrepareClassificationListModel(specificationAttributeOptions);
classificationListModel.Classifications = classifications;
return View(classificationListModel);
}
[HttpPost, GridAction(EnableCustomBinding = true)]
public ActionResult ProductList(GridCommand command, int specificationOptionId)
{
if (!_permissionService.Authorize(StandardPermissionProvider.ManageReports))
return AccessDeniedView();
if (specificationOptionId == 0)
return View();
var gridModel = new GridModel();
IList<int> filterableSpecificationAttributeOptionIds = new List<int> { specificationOptionId };
var products = _productService.SearchProducts(0, 0, null, null, null, 0, null, false,
_workContext.WorkingLanguage.Id, null,
ProductSortingEnum.Position, command.Page, command.PageSize,
true, out filterableSpecificationAttributeOptionIds);
gridModel.Data = products.Select(x =>
{
var productModel = x.ToModel();
return productModel;
});
gridModel.Total = products.TotalCount;
return new JsonResult
{
Data = gridModel
};
}
#endregion
#endregion
}
Okay, so I found the answer. Here is a link to the topic that had it:
Solution
Here are my changes also.
View (Classifications.cshtml)