How to render html passing as controller string to the view?

1

I have the following view:

    public ActionResult RelatorioEquipe()
    {
        ViewRelatorioEquipeDTO dto = new ViewRelatorioEquipeDTO();
        dto.LstUnidadeGerencial = negocio.Relatorio.ObterLstUg();
        dto.LstExercicios = new ExercicioNegocio().ObterExerciciosAtivos();

        dto.Html = "<img src=\"~/Imagens/LogoMetroRelatorio.png\" style=\"float: left\" />";        

        return View(dto);
    }

dto.Html is a string and when I refer to it on my page (cshtml):

<div class="logo">
    @Model.Html
</div>

The page naturally shows a string with this text.

The question is: do you have instead of showing the string, rendering that html and displaying the image?

    
asked by anonymous 04.06.2014 / 22:54

2 answers

0

I found what I was looking for. I was able to render the html in the razor by doing just that:

<div class="logo">
    @Html.Raw(Model.Html)
</div>
    
04.06.2014 / 23:24
2

Yes.

Implement the following extension in your project:

public static class ImageHelper
{
    public static MvcHtmlString Image(this HtmlHelper helper, string src, string altText, string height, string style = "")
    {
        var builder = new TagBuilder("img");
        builder.MergeAttribute("src", src);
        builder.MergeAttribute("alt", altText);
        builder.MergeAttribute("height", height);
        if (style != "") builder.MergeAttribute("style", style);
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }

    public static MvcHtmlString Image(this HtmlHelper helper, string url, string altText, object htmlAttributes)
    {
        TagBuilder builder = new TagBuilder("img");
        builder.Attributes.Add("src", url);
        builder.Attributes.Add("alt", altText);
        builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
        return MvcHtmlString.Create(builder.ToString(TagRenderMode.SelfClosing));
    }
}

Usage:

@Html.Image("caminho/da/imagem", "texto alternativo", "altura", "float: left")

Or else:

@Html.Image("caminho/da/imagem", "texto alternativo", new { style = "float: left", border = "0" })

Do not forget to using in View :

@using SeuProjeto.Extensions

Extensions is an example, but you can put the static class anywhere.

    
04.06.2014 / 23:07
JAAS - request.isUserInRole always returning false ___ ___ erkimt Installing ImageMagick on windows 7 [32-bit]? ______ qstntxt ___
How do I install and configure %code% on windows 7 32bit , and how do I put the commands in it?

In my case, I need to put the command:

%pre%     
______ azszpr20688 ___

For Windows 7, 32 Bit, use the following installation:

ImageMagick for Windows 32 Bit

Install normally with the default options.

After installing, go to the Windows console (cmd).

Type the following command and see if you have the same return as the image below.

%pre%

Iftheabovecommandworks,thenfollowthesesteps:

Step1:Createafolderandplacethefilestoresizewithinit.NotethatinmycasethereareseveralBMPfiles.

Step2 : Create a folder within the current folder. This folder will receive the resized files. I gave the small name, as shown below.

Step3:Runthecommandmogrify-pathsmall-resize50%*.bmp

Noteintheimagebelowthesizeofthefilesbeforetheconversion.Alsonoticethecommandlineabove.

Step4 : Confirmation of the result. Enter the small folder, run a dir and see how the file size has changed.

    
___