简体   繁体   中英

Umbraco Razor - Pass Array to Javascript

I have a foreach loop in Razor which takes images from the Multiple Media Picker in Umbraco. The Response.Write just allows me to see that the images are displaying fine (which they are) so you can ignore this bit.

My question is, how do I populate the image tag with the image URL using the Javascript function? (see below which currently doesn't work).

Razor View/CSHTML

var imagesList = portfolioItem.GetPropertyValue<string>("Images").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var imagesCollection = Umbraco.TypedMedia(imagesList);

foreach (var imageItem in imagesCollection)
{   
    Response.Write("<img src='"+ @imageItem.Url +"' />");
}

Javascript

openInfoWindow = function (name, imagesCollection, location, mw, url, marker) {
    var infoText = "<img src='" + imagesCollection + "' alt='" + imagesCollection + "'title='" + imagesCollection + "' />";
}

try using jquery, if u use razor to set the url as an invisible input value. Then use jquery to pull that value out and add it to the img tag itself by using the following: var inputVal = $("input").val(); $("#my_image").attr("src",inputVal );

if you put the invisble input value as a variable you can set the img atr ursing jquery like I've shown above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM