Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Craig O'Mahony 364 posts 918 karma points
    Nov 27, 2012 @ 13:51
    Craig O'Mahony
    0

    Forcing Razor to render HTML

    Hi,

    I'm using a Razor file to loop through a repository and build up a 'string' which will then be injected into a javascript file.

    The string that I'm trying to return needs to be in the following format:

    aImages[0] = "images/1.jpg";

    aImages[1] = "images/2.jpg";

    aArtists[0] = "Description 1";

    aArtists[1] = "Description 2";

    The cshtml I've written is:-

    @{
        var position = 0;
        var counter = 0;
    }

    @foreach(var item in @Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
        {
            aImages[@position] = "@item.Image";
            position ++;
        }

    @foreach(var item in @Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
        {
            aArtists[@counter] = "@item.ImageDescription";
            counter ++;
        }

    But I'm getting an error of "aImages does not exist in the current context" when I'm saving the script. Which I believe is because Razor is treating this as code and not the plain text that I want it to return. I say this because if I wrap the line in a valid HTML tag it saves as expected and returns the string (plus the HTML tag obviously). What I'm trying to do is return these lines without any markup (as it's script). Is there a way that I can force Razor out of code mode (if that makes sense!)

    Thanks,

    Craig

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 27, 2012 @ 14:02
    Mike Chambers
    0

    <text>XXXX</text>

    or maybe @Html.Raw(XXX)

     

    depending on if you require escaping/non escaping???

  • Mike Chambers 636 posts 1253 karma points c-trib
    Nov 27, 2012 @ 14:06
    Mike Chambers
    0

    Actually...

    In your script above you don't define aImages anywhere???

     

     

    @{
       
    var position =0;
       
    var counter =0;
    string[] aImages;
    srting[] aArtists;

    foreach(var item in@Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
       
    {
            aImages
    [@position]="@item.Image";
            position
    ++;
       
    }

    foreach(var item in@Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
       
    {
            aArtists
    [@counter]="@item.ImageDescription";
            counter
    ++;
       
    }

    maybe

  • Craig O'Mahony 364 posts 918 karma points
    Nov 27, 2012 @ 14:09
    Craig O'Mahony
    0

    aImages in defined in the java I'm just using this to inject a block of text into it.

    Html.Raw was the way to go!

    @{
        var position = 0;
        var counter = 0;
    }

    @foreach(var item in @Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
        {
            @Html.Raw("aImages[" + @position + "] = '" + @item.Image + "';\r\n");
            position ++;
        }

    @foreach(var item in @Model.BackgroundRepositories.First().BackgroundItems.Where("Visible"))
        {
            @Html.Raw("aArtists[" + @counter + "] = '" + @item.ImageDescription + "';\r\n");
            counter ++;
        }

    It's a beautiful thang!!

    Thanks for your help :)

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft