2014-01-08 97 views
2

\ r \ n出現在字符串上?爲什麼 r n添加到我的字符串中?

試圖使用Rendering ASP.NET MVC Views to String上的示例將VIEW作爲字符串傳遞並作爲電子郵件發送。應該通過電子郵件將銷售發票發送給用戶。

Iv'e將ViewRenderer類添加到我的項目中。然後加入ContactSeller功能,我的控制器,並複製和重命名發票視圖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; 
} 

我通過發送電子郵件的功能消失了,我從測試它知道發送電子郵件是不問題。電子郵件包含的是問題。

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; 
} 

所以我breakpointed並檢查了 'viewAsString' 參數傳入:回報:

"\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>&#163;0.00</td>\r\n       <td>3</td>\r\n       <td>&#163;0.00</td>\r\n      </tr>\r\n      <tr>\r\n       <td>BUTTER SQ</td>\r\n       <td>Butternut Squash</td>\r\n       <td>&#163;0.00</td>\r\n       <td>5</td>\r\n       <td>&#163;0.00</td>\r\n      </tr>\r\n      <tr>\r\n       <td>bcf5</td>\r\n       <td>Broccolli &amp; Cauliflour Mix 5kg</td>\r\n       <td>&#163;0.00</td>\r\n       <td>2</td>\r\n       <td>&#163;0.00</td>\r\n      </tr>\r\n      <tr>\r\n       <td>EGGS TRAY</td>\r\n       <td>tray eggs</td>\r\n       <td>&#163;0.00</td>\r\n       <td>1</td>\r\n       <td>&#163;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>&#163;0.00</td>\r\n       <td>9</td>\r\n       <td>&#163;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>&#163;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>&#163;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>&#163;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" 

其中顯示: enter image description here 我的問題是在哪裏都是\ r \ n從哪裏來? 感謝您的任何答覆

UPDATE

public class ConvertOrderViewToStringViewModel 
    { 
     public ConvertOrderViewToStringViewModel() 
     { } 

     public OrderRecord Order; 
     public Dictionary<int, QBProductRecord> Products; 
     public String PathToSignature; 
     public String TypedName; 
     public PaymentRecord Payment; 
    } 
+0

它看起來像所有東西都越獄了,注意'Order Last Last'中的\「以及'title'參數結果。通過調試代碼 – nrathaus

+0

嘗試查看轉義的發生位置您確定已經檢查過'title'參數&not'viewAsString'?因爲'title'參數被傳遞'siteUrl'&'siteUrl'僅用「確認訂單」進行初始化,在此之後它不會被更改,至少這是我所能看到的從你的可用代碼中,我認爲問題在'viewAsString' –

+0

這些是新的行字符,你使用輸入鍵或新行來縮進報表嗎?你可以發佈ConvertOrderViewToStringViewModel構造函數的代碼嗎? –

回答

1

可能是其在Visual Studio中格式化當你的視圖包含空格。

所以,當您轉換您的ViewString他們得到\r\n代表白色空間C# &換行符替換。

您可以嘗試從您的視圖中刪除每個空白區域,這不是必需的。您的視圖中只有html &相關內容。然後通過發送電子郵件再次測試。走着瞧吧。

+0

謝謝Devraj ...正確答案 – John

相關問題