MVC - How to load a template html page, modify it and send it by email

0

I am making a form that sends an email to the client, this email comes from a template depending on the stage of the client. Ex: Stage 01 sending the email with the body coming from template_01.html, if the client is in stage 04, sending the email with template_04.html

I'm doing a format that works, however as MVC is news to me, sometimes I think I'm doing it in hardcore form. Sometimes there is a simpler and unfamiliar means.

How do I do: Within the view in question I create a template_01.html file

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
    <body>
        <p>Prezado Cliente [Nome_Cliente],</p>
        <p>Estamos lhe enviando o andamento do seu site conosco.</p>
        <p>Novo status atualizado em:</p>
        <p>   [Mostra_EP] </p>
        <p>[data]<p>
    </body>
</html>

In the controller I called a library that I created that sends the email. First I read through FileSystem

var conteudo = System.IO.File.ReadAllText(Caminho);

Then I replace the fields [CustomerName], [Show_EP], [data]

conteudo = conteudo.Replace("[Nome_Cliente]", cliente.Nome);
conteudo = couteudo.Replace("[data],DateTime.Now);

In the [Show_EP] it is a bit more complex because I make a query in the database and a foreach and I'm putting together an html. after that I submit this content on:

 objEmail.Body = conteudo;

Is there anything simpler? type a Partial that I calling it it would already return me an html already ready (it executes and returns an html) because the part of [Show_EP] is getting giant and I have not finished yet.

    
asked by anonymous 17.02.2016 / 16:49

1 answer

2

Your reasoning is correct, you're doing a lot of hardcore.

If you are using Asp.net MVC (I assume you are also using Razor Views), you can use the razor feature to render this html for you.

.cshtml file:

@model SuaModelClasse
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
</head>
<body>
    <p>Prezado Cliente @model.NomeCliente,</p>
    <p>Estamos lhe enviando o andamento do seu site conosco.</p>
    <p>Novo status atualizado em:</p>
    <table>
        <tr>
            <th>Col1</th>
            <th>Col2</th>
        </tr>
        @foreach (var item in Model.Itens)
        {
            <tr>
                <td>@item.Col1</td>
                <td>@item.Col2</td>
            </tr>
        }
    </table>
    <p>@Model.Data<p>
</body>
</html>

This is an example. You can use all the features of Razor Views. Then render your view into a string and put the string in the email body.

This can be done by following the this question.

Below the code taken from the response quoted above:

public string RenderRazorViewToString(string viewName, object model)
{
    ViewData.Model = model;

    using (var sw = new StringWriter())
    {
        var viewResult = ViewEngines.Engines
            .FindPartialView(ControllerContext, viewName);

        var viewContext = new ViewContext(
            ControllerContext, viewResult.View, ViewData, TempData, sw);

        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}
    
18.02.2016 / 01:10