Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using resources with error message in mvc

I want to get error message from resources. When i tried codes below i take that error:"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" What can i do ?

public class UserLoginModel
{
     [Required(ErrorMessage =Resources.PageResources.enterYourEmail)]
     public String Email{ get; set; }

     [Required(ErrorMessage =Resources.PageResources.enterPassword)]
     public String Password { get; set; 
}
like image 930
Merve Kaya Avatar asked Jul 30 '13 07:07

Merve Kaya


2 Answers

Try this

 [Required(ErrorMessageResourceType = typeof(Resources.Resources),       
            ErrorMessageResourceName = "enterYourEmail")]
 public String Email{ get; set; }

 [Required(ErrorMessageResourceType = typeof(Resources.Resources),       
            ErrorMessageResourceName = "enterPassword")]
 public String Password { get; set; 
like image 145
Amit Avatar answered Nov 16 '22 22:11

Amit


A similar approach but without the problem related by @Ali Baig about to have to add ErrorMessage = null to make it work.

    [Required(ErrorMessageResourceName = nameof(Resources.Global_Errors.ReqName),
 ErrorMessageResourceType = typeof(Resources.Global_Errors))]
like image 20
AFetter Avatar answered Nov 16 '22 22:11

AFetter