MVC 5 - 如何在控制器中显示枚举显示名称,以便我可以在电子邮件中使用它?

时间:2015-09-12 09:12:34

标签: asp.net-mvc enums asp.net-mvc-5

如何在控制器中显示枚举显示名称,以便我可以在电子邮件中使用它?

注意:我需要控制器和显示器中的显示名称。不是一个观点..

目前我的电子邮件显示如下:

年龄范围:2130岁

但我希望它使用显示名称,而不是:

年龄范围:21-30

以下是我的模态的一个例子:

    public enum AgeGroups
    {
        [Display(Name = "21-30")]
        age2130,
        [Display(Name = "31-40")]
        age3140,
        [Display(Name = "41-50")]
        age4150,
        [Display(Name = "51+")]
        age51plus
    }
    [Required]
    [Display(Name = "Age Group")]
    public AgeGroups? AgeGroup { get; set; }

以下是我的控制器的电子邮件部分:

string fromURL = this.Request.ServerVariables["http_referer"];
string userIpAddress = this.Request.ServerVariables["REMOTE_ADDR"];
var subject = "New enquiry from {0} {1}";
var body = "<p>You have a new enquiry from: {0}</p><p>Interested In: {1}</p><p>First Name: {2}</p><p>Last Name: {3}</p><p>Phone Number: {4}</p><p>Email Address: {5}</p><p>Suburb: {6}</p><p>Post Code: {7}</p><p>Age Group: {8}</p><p>Found Us By: {9}</p><p>Join Mailing List: {10}</p><p>IP Address: {11}</p>";
var replyto = model.EmailAddress;
var message = new MailMessage();
message.To.Add(new MailAddress("test@test.com"));
message.ReplyToList.Add(new MailAddress(replyto));
message.Subject = string.Format(subject, model.FirstName, model.LastName);
message.Body = string.Format(body, fromURL, model.InterestedIn, model.FirstName, model.LastName, model.PhoneNumber, model.EmailAddress, model.Suburb, model.PostCode, model.AgeGroup, model.FoundUs, model.JoinMailingList, userIpAddress);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
    await smtp.SendMailAsync(message);
    return new HttpStatusCodeResult(HttpStatusCode.OK);
}

0 个答案:

没有答案