2017-04-12 28 views
0

在我的應用程序中,您可以將任務(TaskDetails)分配給一個人(員工),然後他/她的電子郵件地址從包含任務。我爲此使用郵政。使用Postal創建的電子郵件包含數據庫查詢而不是字符串值

在創建方法TaskDetails我說:

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Create(TaskDetails taskDetails) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Tasks.Add(taskDetails); 
     db.SaveChanges(); 
     //Extra code for email notification to Employee when new Task is created 
     //See: http://aboutcode.net/postal 
     var emplName = from e in db.Employees 
         where e.EmployeeId.Equals(taskDetails.EmployeeId) 
         select e.FirstName; 
     try 
     { 
      var email = new NewTaskEmail 
      { 
       ToEmail = "[email protected]" 
       ToFirstname = emplName.toString() 
      }; 
      email.Send(); 
     } 
     catch (Exception ex) 
     { 
      return View("Error", new HandleErrorInfo(ex, "TaskDetails", "Create")); 
     } 
     //end of extra code for email notification 
     return RedirectToAction("Index"); 
    } 
//some viewbag code 
    return View(taskDetails); 
} 

我的電子郵件地址的看法:

@model MyApp.Models.NewTaskEmail 

From: [email protected] 
To: @Model.ToEmail 
Subject: You have a new task 

Hi @Model.ToFirstname, 
//rest of email body 

調試時,我可以看到ToEmail和ToFirstname有正確的價值,但這是導致電子郵件:

X-Sender: [email protected] 
X-Receiver: [email protected] 
MIME-Version: 1.0 
From: [email protected] 
To: [email protected] 
Date: 12 Apr 2017 21:11:33 +0200 
Subject: You have a new task 
Content-Type: text/plain; charset=us-ascii 
Content-Transfer-Encoding: quoted-printable 

Hi SELECT =0D=0A [Extent1].[FirstName] AS [FirstName]=0D=0A = 
FROM [dbo].[Employee] AS [Extent1]=0D=0A WHERE [Extent1].[Emp= 
loyeeId] =3D @p__linq__0,=0D=0A=0D=0A 

硬編碼字符串被正確識別,但字符串t由db查詢(Employee.FirstName)產生的帽子顯示爲查詢本身。 我在做什麼錯?

回答

0
var emplName = from e in db.Employees 
    where e.EmployeeId.Equals(taskDetails.EmployeeId) 
    select e.FirstName; 

emplName是字符串的集合,所以你需要使用FirstOrDefault()

var emplName = (from e in db.Employees 
    where e.EmployeeId == taskDetails.EmployeeId 
    select e.FirstName).FirstOrDefault(); 
+0

那是就是,謝謝你:-) – Noor

+0

歡迎您。如果這回答了問題,請將其標記爲*答案*。它會幫助社區。 – Win

相關問題