在Nop Commerce中更新现有实体时出错

时间:2016-11-27 03:39:31

标签: asp.net-mvc nopcommerce

我正在关注教程Updating an existing entity. How to add a new property

我能够成功更改数据模型。但是当我尝试更改演示模型时,我遇到了以下错误。我该如何解决?

CategoryValidator:

我对源代码(演示文稿)所做的更改。

namespace Nop.Admin.Validators.Catalog
{
   public partial class CategoryValidator : BaseNopValidator<CategoryModel>
   {
      public CategoryValidator(ILocalizationService localizationService, IDbContext dbContext)
      {
        RuleFor(x => x.Name).NotEmpty().WithMessage(localizationService.GetResource("Admin.Catalog.Categories.Fields.Name.Required"));
        RuleFor(x => x.PageSizeOptions).Must(ValidatorUtilities.PageSizeOptionsValidator).WithMessage(localizationService.GetResource("Admin.Catalog.Categories.Fields.PageSizeOptions.ShouldHaveUniqueItems"));

        RuleFor(m => m.SomeNewProperty).Length(0, 255);

        SetStringPropertiesMaxLength<Category>(dbContext);
    }
}
}

目录型号:

namespace Nop.Admin.Models.Catalog
{
  [Validator(typeof(CategoryValidator))]
  public partial class CategoryModel : BaseNopEntityModel, ILocalizedModel<CategoryLocalizedModel>
  {
   //other code not posted in question
  [NopResourceDisplayName("Admin.Catalog.Categories.Fields.SomeNewProperty")]
    public string SomeNewProperty { get; set; }
}
}

查看页面:

   <div class="form-group">
      <div class="col-md-3">
         @Html.NopLabelFor(model=> model.SomeNewProperty)
        </div>
       <div class="co-md-9">
          @Html.NopEditorFor(model=>model.SomeNewProperty)
          @Html.ValidationMessageFor(model=>model.SomeNewProperty)
        </div>
     </div>

错误:

Server Error in '/' Application.

Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: length

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: Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: length


Source Error: 


Line 414:            htmlAttributes = new { @class = "form-control" };
Line 415:
Line 416:            result.Append(helper.EditorFor(expression, new { htmlAttributes }));
Line 417:
Line 418:            return MvcHtmlString.Create(result.ToString());

Source File: c:\newfolder\nopCommerce\nopCommerce_3.80_Source\Presentation\Nop.Web.Framework\HtmlExtensions.cs Line: 416 

Stack Trace: 


[InvalidOperationException: Validation type names in unobtrusive client validation rules must be unique. The following validation type was seen more than once: length]
    System.Web.Mvc.UnobtrusiveValidationAttributesGenerator.ValidateUnobtrusiveValidationRule(ModelClientValidationRule rule, IDictionary`2 resultsDictionary, String dictionaryKey) +910
       System.Web.Mvc.UnobtrusiveValidationAttributesGenerator.GetValidationAttributes(IEnumerable`1 clientRules, IDictionary`2 results) +135
       System.Web.Mvc.HtmlHelper.GetUnobtrusiveValidationAttributes(String name, ModelMetadata metadata) +265
       System.Web.Mvc.Html.InputExtensions.InputHelper(HtmlHelper htmlHelper, InputType inputType, ModelMetadata metadata, String name, Object value, Boolean useViewData, Boolean isChecked, Boolean setId, Boolean isExplicitValue, String format, IDictionary`2 htmlAttributes)
+723
       System.Web.Mvc.Html.DefaultEditorTemplates.HtmlInputTemplateHelper(HtmlHelper html, String inputType, Object value) +68

1 个答案:

答案 0 :(得分:0)

此错误是因为您已多次添加验证 长度

只需更改此行:

this.Property(m => m.SomeNewProperty).HasMaxLength(255).IsOptional();  

要:

this.Property(m => m.SomeNewProperty).IsOptional();