ドロップダウンリストがあるページで作業しています。ユーザーが選択を行うと、クライアント側の変更イベントは、ドロップダウンリストの選択に基づいてTelerikグリッドをロードします。現在発生している問題は、ページを読み込もうとすると、次のエラーが発生することです。
オブジェクト参照がオブジェクト インスタンスに設定されていません。説明:現在のWebリクエストの実行中に未処理の例外が発生しました。エラーとそれがコードのどこで発生したかについての詳細は、スタックトレースを確認してください。
例外の詳細:System.NullReferenceException:オブジェクト参照がオブジェクトのインスタンスに設定されていません。
ソースエラー:
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 =>
以下は私のコードのすべてです。何かアイデアがあればありがたいです。ありがとう。
モデル(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; }
}
表示(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>
}
コントローラー(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
}