从数据库中检索数据并通过ASP.NET MVC中的函数调用传递数据

时间:2020-06-03 10:00:37

标签: asp.net entity-framework model-view-controller

我正在使用存储在数据库中的数据执行发送电子邮件功能。 这就是流程...

  1. 单击电子邮件按钮,它将使用ajax将OrderNum作为id传递给SendMailToUser() enter image description here

2。在SendMailToUser()中,它将基于相同的id从数据库中获取数据,并将检索到的数据返回到SendData()

        [HttpGet]
    public JsonResult SendMailToUser(int id) // Get data from database
    {
        using (DEdbEntities1 db = new DEdbEntities1())
        {
            return SendData(db.deorders.Where(x => x.orderNum == id).FirstOrDefault<deorder>());
        }
    }
  1. 然后在SendData()中,它需要在SendEmail()参数中的字符串之间传递数据。我不知道如何在字符串之间插入数据。 {{order.email}}是需要插入的数据。

        public JsonResult SendData(deorder order) //Send data through parameter
    {
        bool result = false;
    
        result = SendEmail("{{order.email}}", "Your package is on the way", "<p>Hi {{order.name}},<br /> Your product has been shipped out<br /> Regards, <br />Destiny Electronic</p>");
    
    
        return Json(result, JsonRequestBehavior.AllowGet);
    
    }
    
  2. 然后它将使用传递的参数执行发送电子邮件功能。该功能没有问题。

       public bool SendEmail(string toEmail, string subject, string emailBody)
    {
        try
        {
            string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
            string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();
    
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(senderEmail, senderPassword);
    
            MailMessage mailMessage = new MailMessage(senderEmail, toEmail, subject, emailBody);
            mailMessage.IsBodyHtml = true;
            mailMessage.BodyEncoding = UTF8Encoding.UTF8;
            client.Send(mailMessage);
    
            return true;
        }
        catch(Exception )
        {
            return false;
        }
    }
    

问题是我不确定如何从函数中检索数据以及如何通过参数传递数据。此外,如何在STEP 2和3的参数中的字符串之间插入数据。非常感谢您的帮助:')

0 个答案:

没有答案