Sending Email asp.net mvc 3
Im having a problem using Email.SendMail(). I need to add more details like Event_Location, Event_Start, etc to the "Email Msg" from the Events table in the database but the var item is already called by events. I would like to know if there is a way to put two var i.e events and members either in the foreach loop or another method?
public void Send()
{
var events = from a in db.Members.ToList()
join e in db.Events.ToList() on a.Hobby_Code equals e.Hobby_Code
where a.Postal_Code == e.Postal_Code || e.Event_Status ==("A")
var member= from w in db.Members.TOList()
join z in db.Events.ToList() on w.Event_ID equals z.Event_ID
select w
foreach (var item in events)
Email.SendMail(item.Member_Email_Address,"Subject","Email Msg",false);
}
Model Class for Member
public class Member
{
[Key]
[ScaffoldColumn(false)]
public int Member_ID { get; set; }
[Required(ErrorMessage = "A Section Name is required")]
[DisplayName("Section Name")]
public string Postal_Code { get; set; }
[Required(ErrorMessage = "An email address is required")]
[DisplayName("Email Address")]
public string Member_Email_Address { get; set; }
[DisplayName("Hobby Code")]
[Required(ErrorMessage = "A Hobby is required")]
public string Hobby_Code { get; set; }
[DisplayName("Event ID")]
public int? Event_ID { get; set; }
public string FullName
{
get
{
return Member_Last_Name + " " + Member_First_Name;
}
}
public virtual Section Section { get; set; }
public virtua开发者_如何转开发l Event Event { get; set; }
}
Model Class for Event
public class Event
{
[ScaffoldColumn(false)]
[Key]
public int Event_ID { get; set; }
[Required(ErrorMessage = "A Event Location is required")]
[DisplayName("Event Location")]
public string Postal_Code { get; set; }
[DisplayName("Event Status")]
public string Event_Status { get; set; }
[DisplayName("Event Start Date")]
[Required]
public DateTime Event_Start { get; set; }
[DisplayName("Event End Date")]
[Required]
public DateTime Event_End { get; set; }
[DisplayName("Event Type")]
[Required(ErrorMessage = "A Event Type is required")]
public string Hobby_Code { get; set; }
[Required]
[StringLength(100)]
[DataType(DataType.MultilineText)]
[DisplayName(" Event Description")]
public string Event_Description { get; set; }
you can use an anonymous method to return only those properties you want, i.e.:
var events = from a in db.Members.ToList()
join e in db.Events.ToList() on a.Hobby_Code equals e.Hobby_Code
where a.Postal_Code == e.Postal_Code || e.Event_Status ==("A")
select new { email = a.Member_Email_Address, location = e.Event_Location, eventStart = e.Event_Start};
and then access it in the for loop like so:
foreach (var item in events)
Email.SendMail(item.email,"Subject",String.Format("Location: {0}",item.location),false); // including all the other variables required and excluding the formatting
If you use ASP .NET MVC's automatic entity mapping properly, it will do all the joins for you. In Member you should have a reference to Event event
rather than Event_ID
. Then you can simply do:
foreach (var member in db.Members)
{
String msgBody = "You are invited to " && member.event.Event_Description && " on " && member.event.Event_Start;
Email.SendMail(member.Member_Email_Address,"Subject", msgBody, false);
}
精彩评论