相关疑难解决方法(0)

如何将ASP.NET MVC视图呈现为字符串?

我想输出两个不同的视图(一个作为将作为电子邮件发送的字符串),另一个显示给用户的页面.

这是否可以在ASP.NET MVC beta中使用?

我尝试过多个例子:

1. ASP.NET MVC Beta中的RenderPartial到String

如果我使用此示例,则会收到"在HTTP标头发送后无法重定向".

2. MVC框架:捕获视图的输出

如果我使用它,我似乎无法执行redirectToAction,因为它尝试渲染可能不存在的视图.如果我确实返回了视图,那么它完全搞砸了,看起来根本不正确.

有没有人对我遇到的这些问题有任何想法/解决方案,或者对更好的问题有任何建议?

非常感谢!

以下是一个例子.我要做的是创建GetViewForEmail方法:

public ActionResult OrderResult(string ref)
{
    //Get the order
    Order order = OrderService.GetOrder(ref);

    //The email helper would do the meat and veg by getting the view as a string
    //Pass the control name (OrderResultEmail) and the model (order)
    string emailView = GetViewForEmail("OrderResultEmail", order);

    //Email the order out
    EmailHelper(order, emailView);
    return View("OrderResult", order);
}
Run Code Online (Sandbox Code Playgroud)

Tim Scott接受的答案(由我改变并格式化):

public virtual string RenderViewToString(
    ControllerContext controllerContext,
    string viewPath, …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc rendering

473
推荐指数
9
解决办法
29万
查看次数

我可以使用ASP.Net MVC Razor视图生成格式良好的HTML Body作为从服务器发送的电子邮件的输入吗?

我想利用Razor View的模型绑定/渲染功能为我从ASP.NET MVC应用程序发送的电子邮件生成HTML正文内容.

有没有办法将视图呈现给字符串而不是将其作为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 …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc view razor

21
推荐指数
2
解决办法
1万
查看次数

标签 统计

asp.net-mvc ×2

razor ×1

rendering ×1

view ×1