更新 Nopcommerce 中的现有实体时出错

Error while Updating an existing entity in Nop Commerce

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

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

类别验证器:

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

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

此错误是因为您被添加验证不止一次 长度

只需更改此行:

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

收件人:

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