大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 可以使用ASP.Net MVC Razor视图来生成格式很好的HTML Body作为从服务器发送的电子邮件的输入吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法将视图呈现为字符串,而不是将其作为GET请求的ActionResult返回?
为了说明我正在寻找将会做以下事情的东西:
public ActionResult SendEmail(int id) { EmailDetailsViewModel emailDetails = EmailDetailsViewModel().CreateEmailDetails(id); // THIS IS WHERE I NEED HelP... // I want to pass my ViewModel (emailDetails) to my View (EmailBodyRazorView) but instead of Rending that to the Response stream I want to capture the output and pass it to an email client. String htmlEmailBody = View("EmailBodyRazorView",emailDetails).ToString(); // Once I have the htmlEmail body I'm good to go. I've got a utilityt that will send the email for me. MyEmailUtility.SmtpSendEmail("stevejobs@apple.com","Email Subject",htmlEmailBody); // Redirect another Action that will return a page to the user confirming the email was sent. return RedirectToAction("ConfirmationEmailWasSent"); }
public String ToHtml(String viewToRender,ViewDataDictionary viewData,ControllerContext controllerContext) { var result = ViewENGInes.ENGInes.FindView(controllerContext,viewToRender,null); StringWriter output; using (output = new StringWriter()) { var viewContext = new ViewContext(controllerContext,result.View,viewData,controllerContext.Controller.TemPDAta,output); result.View.Render(viewContext,output); result.ViewENGIne.ReleaseView(controllerContext,result.View); } return output.ToString(); }
您需要从控制器操作中传递视图的名称以及ViewData和ControllerContext。
以上是大佬教程为你收集整理的asp.net-mvc – 可以使用ASP.Net MVC Razor视图来生成格式很好的HTML Body作为从服务器发送的电子邮件的输入吗?全部内容,希望文章能够帮你解决asp.net-mvc – 可以使用ASP.Net MVC Razor视图来生成格式很好的HTML Body作为从服务器发送的电子邮件的输入吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。