传递到字典中的模型项为null,但此字典需要“System.DateTime”类型的非null模型项

时间:2012-09-19 03:58:30

标签: c# asp.net-mvc nopcommerce

我正在使用nopcommerce开源。 所以我想添加另一个表,我想在Product Variant中添加另一个选项卡。

所以我创建了一个模型。

 public partial class ProductVariantPriceRangeModel : BaseNopEntityModel
        {
            public int ProductVariantId { get; set; }

             [NopResourceDisplayName("Admin.Catalog.Products.Variants.ProductVariantPriceRange.Fields.SpecialPriceStartDate")]
           [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:yyyy-MM-dd}")]

            public DateTime? SpecialPriceStartDate { get; set; }

             [NopResourceDisplayName("Admin.Catalog.Products.Variants.ProductVariantPriceRange.Fields.SpecialPriceEndtDate")]
            [DisplayFormat(NullDisplayText = "", DataFormatString = "{0:yyyy-MM-dd}")]

            public DateTime? SpecialPriceEndtDate { get; set; }


            [NopResourceDisplayName("Admin.Catalog.Products.Variants.ProductVariantPriceRange.Fields.Price")]

            public decimal Price1 { get; set; }
        }

这是我的控制器。

[HttpPost, GridAction(EnableCustomBinding = true)]
        public ActionResult ProductVariantPriceRangeList(GridCommand command, int productVariantId)
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageCatalog))
                return AccessDeniedView();

            var productVariant = _productService.GetProductVariantById(productVariantId);
            if (productVariant == null)
                throw new ArgumentException("No product variant found with the specified id");

            var productVariantPriceRange = productVariant.ProductVariantPriceRange;
            var productVariantPriceRangeModel = productVariantPriceRange
                .Select(x =>
                {
                    return new ProductVariantModel.ProductVariantPriceRangeModel()
                    {                      

                        Id = x.Id,                       
                        ProductVariantId = x.ProductVariantId,
                        SpecialPriceStartDate = x.SpecialPriceStartDate,
                        SpecialPriceEndtDate=x.SpecialPriceEndtDate,                       
                        Price1 = x.SpecialPrice
                    };
                })
                .ToList();

            var model = new GridModel<ProductVariantModel.ProductVariantPriceRangeModel>
            {
                Data = productVariantPriceRangeModel,
                Total = productVariantPriceRangeModel.Count
            };

            return new JsonResult
            {
                Data = model
            };
        }

这是我的观点。

@model ProductVariantModel
@using Telerik.Web.Mvc.UI;
@using Nop.Core.Domain.Catalog;
@using Nop.Admin;
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.ProductId)



@Html.Telerik().TabStrip().Name("productvariant-edit").Items(x =>
{
    x.Add().Text(T("Admin.Catalog.Products.Variants.Info").Text).Content(TabInfo().ToHtmlString()).Selected(true);
    x.Add().Text(T("Admin.Catalog.Products.Variants.TierPrices").Text).Content(TabTierPrices().ToHtmlString());
     x.Add().Text(T("Admin.Catalog.Products.Variants.ProductVariantAttributes").Text).Content(TabProductVariantAttributes().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Products.Variants.Discounts").Text).Content(TabDiscounts().ToHtmlString());

    x.Add().Text(T("Admin.Catalog.Products.Variants.ProductVariantPriceRange").Text).Content(TabProductVariantPriceRange().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "productvariant-edit"));
})



@helper TabProductVariantPriceRange()
    {
      if (Model.Id > 0)
        {
   @(Html.Telerik().Grid<ProductVariantModel.ProductVariantPriceRangeModel>()
                        .Name("productVariantPriceRange-grid")
                .DataKeys(keys =>
                {
                    keys.Add(x => x.Id);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax()
                        .Select("ProductVariantPriceRangeList", "ProductVariant", new { productVariantId = Model.Id })
                        .Insert("ProductVariantPriceRangeInsert", "ProductVariant", new { productVariantId = Model.Id })
                        .Update("ProductVariantPriceRangeUpdate", "ProductVariant")
                       .Delete("ProductVariantPriceRangeDelete", "ProductVariant");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.ProductVariantId)
                        .Width(100).ReadOnly()
                        .Centered();                    


                    columns.Bound(x => x.SpecialPriceStartDate)
                       .Width(200)
                        .Centered();
                    //columns.Bound(x => x.SpecialPriceEndtDate)
                    //    .Width(200)
                    //    .Centered();

                    columns.Bound(x => x.Price1)
                        .Width(100);
                    columns.Command(commands =>
                    {
                        commands.Edit();
                        commands.Delete();
                    })
                    .Width(180);


                })
                .ToolBar(commands => commands.Insert())
                .ClientEvents(events => events.OnEdit("onTierPriceEdit"))
                .EnableCustomBinding(true))


        }
        else
        {
    @T("Admin.Catalog.Products.Variants.ProductVariantPriceRange.SaveBeforeEdit")
        }

}

当我运行程序员时出现错误..

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
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.InvalidOperationException: The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.

Source Error:


Line 83:       if (Model.Id > 0)
Line 84:         {
Line 85:    @(Html.Telerik().Grid<ProductVariantModel.ProductVariantPriceRangeModel>()
Line 86:                         .Name("productVariantPriceRange-grid")
Line 87:                 .DataKeys(keys =>

我该如何解决?

由于

1 个答案:

答案 0 :(得分:2)

我发现了这个相关的帖子:

Telerik MVC Grid - problem with nullable DateTime property

您需要修改Views / Shared中的编辑器模板以支持可为空的日期时间。