\r\n appearing on string?
Attempting to use the example on Rendering ASP.NET MVC Views to String to pass a VIEW as a string, and send as an Email. Which should send the invoice of a sale through email to the user.
Iv'e added ViewRenderer class to my project. Then added the ContactSeller function to my controller, and copied and renamed the invoice view as ViewOrderThroughEmail.cshtml
//confirm payment & send invoice as email:
SendOrderInvoiceAsEmail(Qbcust, orderId);
private void SendOrderInvoiceAsEmail(QBCustomerRecord QBcust, int orderId)
{
string email = QBcust.EmailAddress;
if (String.IsNullOrWhiteSpace(email))
{
_orchardServices.Notifier.Error(T("No E-mail exists for this user. Invoice not sent."));
return;
}
var siteUrl = "Confirmation of order";
if (_authenticationService.GetAuthenticatedUser() == null)
throw new OrchardSecurityException(T("Login Required"));
IUser userInfo = _authenticationService.GetAuthenticatedUser();
QBCustomerRecord customer = _qbcustomerService.GetById(QBcust.Id);
IEnumerable<OrderRecord> ordersQuery = _orderService.GetOrders(customer.Id);
OrderRecord order = ordersQuery.Where(o => o.Id == orderId).FirstOrDefault();
var ids = order.Details.Select(d => d.ProductId);
ConvertOrderViewToStringViewModel model = new ConvertOrderViewToStringViewModel { Order = _orderService.GetOrder(orderId), Products = ProductHelper.GetProductLookup(ids, _qbProductService, true) };
model.Order = _orderService.GetOrder(orderId);
OrderSignatureRecord signatureRecord = _signatureService.GetByOrderId(orderId);
if (signatureRecord != null)
{
model.PathToSignature = _mediaService.GetPublicUrl(signatureRecord.PathToImageFile);
model.TypedName = signatureRecord.TypedName;
}
model.Payment = _paymentService.GetPaymentByOrderId(orderId);
string orderInvoiceEmailAsString = ConvertViewToString(model);
_userService.SendEmail(email, siteUrl, orderInvoiceEmailAsString);
_orchardServices.Notifier.Information(T("The user will receive a confirmation of their order through email."));
}
public string ConvertViewToString(ConvertOrderViewToStringViewModel model)
{
string message = ViewRenderer.RenderView("~/Modules/StormTree/Views/Account/ViewOrderThroughEmail.cshtml",model, ControllerContext);
return message;
}
I've gone through the sending email function, and I know from testing it that sending the email is not the problem. What the email contains is the problem.
public bool SendEmail(string usernameOrEmail, string title, string viewAsString)
{
var lowerName = usernameOrEmail.ToLowerInvariant();
var user = _contentManager.Query<UserPart, UserPartRecord>().Where(u => u.NormalizedUserName == lowerName || u.Email == lowerName).List().FirstOrDefault();
if (user != null)
{
string nonce = CreateNonce(user, DelayToResetPassword);
//string url = createUrl(nonce);
string url = title;
_messageManager.Send(user.ContentItem.Record, MessageTypes.LostPassword, "email", new Dictionary<string, string> { { "LostPasswordUrl", url } });
return true;
}
return false;
}
So I've breakpointed and checked the 'viewAsString' parameter passed in: returns:
"\r\n\r\n<div class=\"span6\">\r\n <fieldset>\r\n <h2 style=\"color:Red;\">Order details</h2>\r\n <table style=\"border-bottom-color:Lime;\" class=\"table-bordered table-striped table\">\r\n <colgroup>\r\n <col id=\"Col1\" />\r\n <col id=\"Col2\" />\r\n <col id=\"Col3\" />\r\n <col id=\"Col4\" />\r\n <col id=\"Col5\" />\r\n </colgroup>\r\n <thead>\r\n <tr>\r\n <th scope=\"col\">Code</th>\r\n <th scope=\"col\">Product</th>\r\n <th scope=\"col\">Unit Price</th>\r\n <th scope=\"col\">Quantity</th>\r\n <th scope=\"col\">Value</th>\r\n </tr>\r\n </thead>\r\n <tbody>\r\n <tr>\r\n <td>APPBRAMSI</td>\r\n <td>Single cooking apples</td>\r\n <td>£0.00</td>\r\n <td>3</td>\r\n <td>£0.00</td>\r\n </tr>\r\n <tr>\r\n <td>BUTTER SQ</td>\r\n <td>Butternut Squash</td>\r\n <td>£0.00</td>\r\n <td>5</td>\r\n <td>£0.00</td>\r\n </tr>\r\n <tr>\r\n <td>bcf5</td>\r\n <td>Broccolli & Cauliflour Mix 5kg</td>\r\n <td>£0.00</td>\r\n <td>2</td>\r\n <td>£0.00</td>\r\n </tr>\r\n <tr>\r\n <td>EGGS TRAY</td>\r\n <td>tray eggs</td>\r\n <td>£0.00</td>\r\n <td>1</td>\r\n <td>£0.00</td>\r\n </tr>\r\n <tr>\r\n <td>savoy single</td>\r\n <td>Savoy Cabbage 1*</td>\r\n <td>£0.00</td>\r\n <td>9</td>\r\n <td>£0.00</td>\r\n </tr>\r\n <tr>\r\n <td></td>\r\n <td></td>\r\n <td></td>\r\n <td><strong>Sub-Total</strong></td>\r\n <td><strong>£0.00</strong></td>\r\n </tr>\r\n <tr>\r\n <td></td>\r\n <td></td>\r\n <td></td>\r\n <td><strong>VAT</strong></td>\r\n <td><strong>£0.00</strong></td>\r\n </tr>\r\n <tr>\r\n <td></td>\r\n <td></td>\r\n <td></td>\r\n <td><strong>Total</strong></td>\r\n <td><strong>£0.00</strong></td>\r\n </tr>\r\n </tbody>\r\n </table>\r\n </fieldset>\r\n</div>\r\n\r\n<div class=\"span4\">\r\n <fieldset>\r\n <h2>Order information</h2>\r\n\r\n <div class=\"editor-label\"><label for=\"Order_CreatedAt\">Order Last Updated</label></div>\r\n <div class=\"editor-field\"><input class=\"uneditable-input\" id=\"Order_CreatedAt\" name=\"Order.CreatedAt\" readonly=\"readonly\" type=\"text\" value=\"19/12/2013 17:19:53\" /></div>\r\n\r\n <div class=\"editor-label\"><label for=\"Order_Status\">Status</label></div>\r\n <div class=\"editor-field\"><input class=\"uneditable-input\" id=\"Order_Status\" name=\"Order.Status\" readonly=\"readonly\" type=\"text\" value=\"New\" /></div>\r\n\r\n\r\n\r\n <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin/AddToCurrentOrder/171\" title=\"Add this order to Current Order\">Add this order to Current Order</a></p>\r\n <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin/ClearThenAddToCurrentOrder/171\" title=\"Change Current Order To Match This Order\">Change Current Order To Match This Order</a></p>\r\n <p><a href=\"/OrchardLocal/UMACS.Bradmount/CustomerAdmin\" title=\"Back to My Account\">Go back to My Account</a></p>\r\n </fieldset>\r\n</div>\r\n\r\n"
which displays: My question being where are all the \r\n coming out of? Thanks for any replies
UPDATE
public class ConvertOrderViewToStringViewModel
{
public ConvertOrderViewToStringViewModel()
{ }
public OrderRecord Order;
public Dictionary<int, QBProductRecord> Products;
public String PathToSignature;
public String TypedName;
public PaymentRecord Payment;
}
May be its the white spaces that your view contains when formatted in visual studio.
So when you convert your
View
toString
they get replaced by\r\n
which represents white space & new line characters inC#
.You can try removing every white space from your view, which is not required. Have only html & relevant content in your view. Then test again by sending an email. See what happens.