如何从局部视图生成html?

时间:2011-12-12 15:24:28

标签: asp.net .net asp.net-mvc asp.net-mvc-3

我有一些动作。我想发送邮件。通常我这样做。

public ActionResult Action(Model model)
{
    string body = 
             "<html>"+
             "<head>"+
             "</head>"+
             "<body>"+
             "</body>"+
             "</html>"

    MailMessage Message = new MailMessage();
    Message.IsBodyHtml = true;
    Message.Subject = "some subject";
    Message.Body = body;
    Message.To.Add(new MailAddress(adres));
    Message.BodyEncoding = Encoding.GetEncoding("utf-8");
    SmtpClient Smtp = new SmtpClient();
    Smtp.EnableSsl = false; 
    Smtp.Send(Message);

    return RedirectToAction("SomeAction","Controller");
}

可以说我如何从PartialView生成HTML并通过电子邮件发送此html?

类似这样的事情

public ActionResult Action(Model model)
{    
    string str = GetHtmlFromPartialView("NamePartialView",model);


    MailMessage Message = new MailMessage();
    Message.IsBodyHtml = true;
    Message.Subject = "some subject";
    Message.Body = str;
    Message.To.Add(new MailAddress(adres));
    Message.BodyEncoding = Encoding.GetEncoding("utf-8");
    SmtpClient Smtp = new SmtpClient();
    Smtp.EnableSsl = false; 
    Smtp.Send(Message);

    return View();
}

2 个答案:

答案 0 :(得分:2)

结帐MvcMailer。斯科特汉塞尔曼也blogged。您将看到使用它发送电子邮件更容易和有趣。邮件模板被定义为Razor视图。

答案 1 :(得分:1)