如何在Umbraco的Jquery中获取多媒体选择器

时间:2016-07-13 13:30:59

标签: jquery umbraco

这是我的脚本,它从Jquery

加载图像滑块
<script type="text/javascript">
        $(window).load(function () {

            @{
                var galleryList = Model.Content.GetPropertyValue<string>("images").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
                var galleryCollection = Umbraco.TypedMedia(galleryList).Where(x => x != null).ToString();
            }

            var i = 0;
            var images = @galleryCollection;
            //var images = ['/Images/Image1.jpg', '/Images/Image2.jpg', '/Images/Image3.jpg', '/Images/Image4.jpg'];
            var image = $('#slideit');
            //Initial Background image setup
            image.css('background-image', 'url(/Images/image1.jpg)');
            //Change image at regular intervals
            setInterval(function () {
                image.fadeOut(0, function () {
                    image.css('background-image', 'url(' + images[i++] + ')');
                    image.fadeIn(0);
                });
                if (i == images.length)
                    i = 0;
            }, 5000);
        });
    </script>

返回错误:未终止的模板文字

  

var images =   System.Linq.Enumerable + WhereEnumerableIterator`1 [Umbraco.Core.Models.IPublishedContent];

我知道这是在jquery中从umbraco获取图像的错误方法。但我不知道其他方式。

1 个答案:

答案 0 :(得分:1)

它应该是这样的:

@using System.Web.Script.Serialization

<script type="text/javascript">
    $(window).load(function () {

        @{
            var galleryList = Model.Content
                                   .GetPropertyValue<string>("images")
                                   .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)
                                   .Select(id => int.Parse(id));

            var galleryCollection = Umbraco.TypedMedia(galleryList)
                                           .Select(m => m.Url);
        }

        var i = 0;
        var images = @(Html.Raw(new JavaScriptSerializer().Serialize(galleryCollection)));

        ... //the rest of your code
    });
</script>