Either ErrorMessageString or ErrorMessageResourceName must be set, but not both error using CreditCardAttribute

前端 未结 8 1950
囚心锁ツ
囚心锁ツ 2020-12-24 04:44

This is my model:

namespace MvcApplication2.Models
{
    public class CreditCard
    {
        [CreditCard(ErrorMessageResourceType = typeof(Messages), Error         


        
8条回答
  •  爱一瞬间的悲伤
    2020-12-24 05:28

    I had this problem because I had implemented a RequiredAttributeAdapter, which was setting the ErrorMessageResourceName property automatically.

    You can fix it by checking to see if the ErrorMessage property has been set before setting the ErrorMessageResourceName:

    /// 
    /// Creates a new instance of the RequiredAttributeAdapter, used for switching the default required message
    /// format
    /// 
    public CustomMessageRequiredAttributeAdapter(
        ModelMetadata metadata,
        ControllerContext context,
        RequiredAttribute attribute
    )
        : base(metadata, context, attribute)
    {
        if (string.IsNullOrEmpty(attribute.ErrorMessage))
        {
            attribute.ErrorMessageResourceType = typeof (ValidationMessages);
            attribute.ErrorMessageResourceName = "PropertyValueRequired";
        }
    }
    

提交回复
热议问题