C# 如何将字典键和值作为字符串C分配给变量#
我正在尝试发送电子邮件,并将字典键和值作为字符串分配给变量C# 如何将字典键和值作为字符串C分配给变量#,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试发送电子邮件,并将字典键和值作为字符串分配给变量 public ActionResult SendContactUs(ContactUsModel model) { SendEmail sendemail = new SendEmail(); string toEmail = ConfigurationManager.AppSettings["ContactUsEmail"]; var keys = new Dictionar
public ActionResult SendContactUs(ContactUsModel model)
{
SendEmail sendemail = new SendEmail();
string toEmail = ConfigurationManager.AppSettings["ContactUsEmail"];
var keys = new Dictionary<string, string>() {
{ "Firstname", model.Firstname },
{ "Lastname", model.Lastname },
{ "Email", model.Email },
{ "Orgnization", model.Orgnization },
{ "Message", model.Message }
};
//here i want to pass keys to body as string
string body = keys ;
sendemail.Send(new EmailModel()
{
Body = body,
To = new List<string>() { toEmail },
});
return Json(new { val = true }, JsonRequestBehavior.AllowGet);
}
可以使用Linq构造字符串:
string body = string.Join(Environment.NewLine, keys.Select(x=>$"{x.Key}: {x.Value}"));
可以使用Linq构造字符串:
string body = string.Join(Environment.NewLine, keys.Select(x=>$"{x.Key}: {x.Value}"));
如果你唯一使用字典的地方是电子邮件文本,那么不要使用字典——只需构建一个字符串?那为什么还要使用字典呢?根据需要使用String/StringBuilder。如果你唯一使用字典的地方是电子邮件文本,那么不要使用字典-只需构建字符串?为什么还要使用字典呢?根据需要使用字符串/字符串生成器。