我正在制作一个有下拉列表的页面。一旦用户做出选择,客户端更改事件将根据下拉列表的选择加载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>
}
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
}
答案 0 :(得分:0)
好的,所以我找到了答案。以下是该主题的链接:
以下是我的更改。
查看(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">
<div id="divGrid" style="display:none">
@(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))
</div>
</td>
</tr>
</table>
<script type="text/javascript">
var initialLoad = true;
$("#Classification").change(function () {
$("#divGrid").show();
$("#classification-products-grid").data("tGrid").rebind();
});
function onDataBinding(e) {
if (initialLoad == true) {
e.preventDefault();
initialLoad = false;
}
else {
var classificationId = $('#@Html.FieldIdFor(model => model.Classification)').val();
if (classificationId != "")
e.data = $.extend(e.data, {
specificationOptionId: classificationId
});
else {
e.preventDefault();
$("#divGrid").hide();
}
}
}
</script>
}