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!)
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:-
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
<text>XXXX</text>
or maybe @Html.Raw(XXX)
depending on if you require escaping/non escaping???
Actually...
In your script above you don't define aImages anywhere???
maybe
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
is working on a reply...