2012-09-18 46 views
48

兩個錯誤這是我的模型:無論ErrorMessageString或ErrorMessageResourceName必須設置,但不使用CreditCardAttribute

namespace MvcApplication2.Models 
{ 
    public class CreditCard 
    { 
     [CreditCard(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "CardNumberInvalid")] 
     public string CardNumber { get; set; } 
    } 
} 

這是我Messages.resx:

名稱值

CardNumberInvalid檢查你的卡號碼正確

這是我的看法:

@model MvcApplication2.Models.CreditCard 
@Html.TextBoxFor(m => m.CardNumber); 

在MVC版本3中,這個工作沒有錯誤。在MVC 4中,當我進入這個頁面時,我得到一個消息:「ErrorMessageString或ErrorMessageResourceName必須設置,但不能同時設置」。這隻會發生在CreditCardAttribute中。其他驗證屬性(如RequiredAttribute)正常工作。我只設置了ErrorMessageResourceName。我沒有設置ErrorMessageString,所以不明白我做錯了什麼。任何人都可以幫忙嗎?

回答

147

這是.NET 4.5中的一個已知問題。添加「ErrorMessage = null」命名參數應該解決這個問題。

參考: 參考鏈接現在被打破。 http://connect.microsoft.com/VisualStudio/feedback/details/757298/emailaddress-attribute-is-unable-to-load-error-message-from-resource-mvc

+24

我在使用新的EmailAddress數據註釋時遇到了同樣的錯誤。這成功解決了這個問題。謝謝! –

+1

但是,這也會刪除任何自定義錯誤消息集。檢查Ians回答一個傻瓜的方式! http://stackoverflow.com/a/16211476/268091 – MEMark

+0

@MEMark這是爲了響應需要直接從資源設置錯誤消息。不知道我明白引用自定義錯誤消息。 –

-1

我在本地化的屬性上有同樣的問題。我已經定義的ErrorMessageResourceType然後ErrorMessageResourceName但錯放的ErrorMessage atrtribute上,以及它通過只刪除ErrorMessage我解決了這一問題拋出異常

[NotEqualTo("User_Name", ErrorMessageResourceType = typeof(LROResources.Global), ErrorMessageResourceName = "UserPasswordCannotBeUsername", ErrorMessage = "The password cannot be the same as your Username.")] 

所以在我的情況。

+1

當你看錯誤時,這很明顯。 OP沒有設置錯誤消息,所以它不應該給出錯誤。它所做的原因是[@DipenBhikadya](http://stackoverflow.com/a/12488268/1682559)中提到的數據註釋屬性本身中的錯誤。當你刪除'ErrorMessage'時,你的問題的確解決了你的問題,這個錯誤從給出的錯誤中很清楚,但是它與OP的問題不同(與錯誤信息相同)。 –

10

我有這個問題,因爲我實現了RequiredAttributeAdapter,它自動設置ErrorMessageResourceName屬性。

您可以通過檢查,看是否ErrorMessage屬性已設置ErrorMessageResourceName時已設定修正:

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

這是完美的!我很久以來一直在使用屬性屬性而未成功。 – MEMark

13
[CreditCard(ErrorMessageResourceType = typeof(Messages), ErrorMessageResourceName = "CardNumberInvalid", ErrorMessage = null)] 

添加ErrorMessage = null將解決您的問題。

0

由於任何人使用自定義驗證屬性,並且還想從資源中加載出於本地化目的的錯誤消息,所以會面臨這個問題,我在這裏分享我的解決方法。
假設你有這樣一個

[FileTypeMustBe("jpg")] 
public HttpPostedFileBase MyFile {get; set;} 

自定義驗證屬性,在定製驗證屬性添加該代碼

public override string FormatErrorMessage(string name) 
    { 
     return String.Format(CultureInfo.CurrentCulture, 
      ErrorMessageString, name, ValidFileType); 
    } 

ValidFileType是需要定製的輸入參數屬性的名稱驗證屬性(jpg在這裏),現在我們可以用我們喜歡的方式來裝飾我們的模型屬性

[FileTypeMustBe("jpg", ErrorMessageResourceType = typeof(Resources.Resources), ErrorMessageResourceName = "WrongFileTypeError")] 
public HttpPostedFileBase MyFile {get; set;} 

如您所見,無需再添加ErrorMessage=null,因爲我們在自定義驗證類中處理了它。只是不要忘記,如果您在自定義驗證中初始化了任何錯誤消息字符串,則必須立即將其刪除,並使用FormatErrorMessage代替。

0

我有一個自定義ValidationAttribute類似的問題。

在IsValid方法中,我設置了ErrorMessage。此修復程序是去除分配到的ErrorMessage歡迎使用屬性...

protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
{ 
    //code before ... 
    this.ErrorMessage = this.FormatErrorMessage(validationContext.DisplayName); //NOT GOOD... 
    ValidationResult validationResult = new ValidationResult(this.ErrorMessage, //And using the ErrorMessage Here... 
     new[] { validationContext.MemberName }); 
    return validationResult; 
} 

我書面方式單元測試,他們被pasisng只有當我被一個運行/調試一個。但是當我點擊「全部運行」時,只有第一個傳球?他們不會以任何方式鏈接...

所以呀,只是刪除刪除分配到的ErrorMessage歡迎使用屬性

我希望這會幫助別人!

相關問題