开发者

Using a loop when sending a Postmark message

I am trying to send an email to the user of my site with order details in it. The email would show a list of items that the user has ordered. On web pages I use a foreach loop that displays the list of items. When I try to this within my postmark email message it gives the error: "Invalid expression term 'foreach'". I don't know how to proceed with this and there isn't much postmark documentation to help.

My code:

    //database query
PostmarkMessage cmessage = new PostmarkMessage
{
From = "server@server.co.uk",
To = "user@user.co.uk",
Subject = "Thank you! " + ordernumber,
HtmlBody = "<h2>Your Order:" + ordernumber + "</h2><br />" + 
 foreach (var o in order)//error here
 {
  <div>
  <div>@o.to_C开发者_如何学ChooseCanvasSize</div><div>&pound;@o.to_Price</div>
  <div>@o.to_Amount</div><div>&pound;@o.to_ItemTotalPrice</div></div>
  }
  "end!",
 };

I am working in C# and ASP.NET Web Pages. Thanks


You need to manually assemble a string using a StringBuilder and a separate loop, then set the HtmlBody property to the generated string.

Alternatively, if you're really in a Razor page, you could use an inline helper:

HtmlBody = new Func<object, HelperResult>(@<text>
    <h2>...</h2>
    @foreach(...) { ... }
</text>)(null).ToString()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜