Mvc DisplayAttribute Name based on resources

If you try to use resources in the property Name like this:

    public class ForgotViewModel
    {
        [Required]
        [Display(Name = Resources.Email)]
        public string Email { get; set; }
    }

You will get the error “An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type”

And this error is correct. So to use resources you will have to do something like this:

    public class ForgotViewModel
    {
        [Required]
        [Display(Name = "Email", ResourceType=typeof(Resources))]
        public string Email { get; set; }
    }

Now in your Resources type you will have to make sure that the Email property exists and that it’s Access Modifier is Public.

To may all Resources Public double click in your resx files and locate the drop box “Access Modifier” and change it’s value to Public.

 

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter

Mvc5 EmailService implementation

This my generic EmailService Implementation. From here I can configure any email service like gmail.

public class EmailService : IIdentityMessageService
    {
        private const string ConfigurationEmailIsBodyHtml = "EmailIsBodyHtml";

        public static string From { get; set; }
        public static string UserName { get; set; }
        public static string Password { get; set; }
        public static string Host { get; set; }
        public static int Port { get; set; }
        public static bool EnableSsl { get; set; }
        public static bool IsBodyHtml { get; set; }
        public static bool UseDefaultCredentials { get; set; }
        public static SmtpDeliveryMethod DeliveryMethod { get; set; }

        static EmailService()
        {
            IsBodyHtml = ConfigurationManager.AppSettings[ConfigurationEmailIsBodyHtml].ToOrOther<bool>(true);

            SmtpSection section = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");

            if (section != null)
            {
                From = section.From;
                Host = section.Network.Host;
                Port = section.Network.Port;
                EnableSsl = section.Network.EnableSsl;
                UserName = section.Network.UserName;
                Password = section.Network.Password;
                DeliveryMethod = section.DeliveryMethod;
                UseDefaultCredentials = section.Network.DefaultCredentials;
            }

        }

        public Task SendAsync(IdentityMessage message)
        {

            var smtp = new SmtpClient
            {
                Host = Host,
                Port = Port,
                EnableSsl = EnableSsl,
                DeliveryMethod = DeliveryMethod,
                UseDefaultCredentials = UseDefaultCredentials,
                Credentials = new NetworkCredential(UserName, Password)
            };

            using (var smtpMessage = new MailMessage(From, message.Destination))
            {
                smtpMessage.Subject = message.Subject;
                smtpMessage.Body = message.Body;
                smtpMessage.IsBodyHtml = IsBodyHtml;
                return smtp.SendMailAsync(smtpMessage);
            }

            // Plug in your email service here to send an email.
            //return Task.FromResult(0);
        }
    }

Gmail configuration:

  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="[email protected]">
        <network host="smtp.gmail.com"
                 port="587"
                 enableSsl="true"
                 defaultCredentials="false"
                 userName="[email protected]"
                 password="password"/>
      </smtp>
    </mailSettings>
  </system.net>

 

Digg thisBuffer this pageShare on FacebookShare on Google+Share on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrTweet about this on Twitter