自定义数据类型在umbraco

时间:2017-07-14 07:14:08

标签: c# angularjs razor umbraco umbraco7

我正在尝试创建一个数据类型的轮播, 为此,我试图从开发人员选项卡

创建这样的自定义数据类型

enter image description here

在内容部分中会显示如下

enter image description here

所以我在这样的模板中调用这个新的数据类型

enter image description here

但我在浏览器中得到了像这样的整个对象

enter image description here

响应就像这样来了

enter image description here

任何想法如何在模板中调用它来显示带有文本和图像的轮播。

目前这将返回@ Umbraco.Field(“car”)

整个对象请给我任何建议,我需要显示为旋转木马:)

2 个答案:

答案 0 :(得分:1)

就Umbraco而言,它正在为您提供属性中包含的原始值,您需要解析原始值,或者我建议的方法是实现您自己的PropertyValueConvertor

  

属性值转换器将属性编辑器数据库存储值转换为另一种类型。可以从MVC Razor或任何其他已发布的内容API访问转换后的值。

要实现这一目标,您需要实现IPropertyValueConverter界面。

有很多例子可以展示如何自己实现。这是一个很简单但相关的example取自Umbraco Core:

using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Umbraco.Core.Logging;
using Umbraco.Core.Models.PublishedContent;

namespace Umbraco.Core.PropertyEditors.ValueConverters
{
    /// <summary>
    /// The default converter for all property editors that expose a JSON value type
    /// </summary>
    /// <remarks>
    /// Since this is a default (umbraco) converter it will be ignored if another converter found conflicts with this one.
    /// </remarks>
    [DefaultPropertyValueConverter]
    [PropertyValueType(typeof(JToken))]
    [PropertyValueCache(PropertyCacheValue.All, PropertyCacheLevel.Content)]
    public class JsonValueConverter : PropertyValueConverterBase
    {
        /// <summary>
        /// It is a converter for any value type that is "JSON"
        /// </summary>
        /// <param name="propertyType"></param>
        /// <returns></returns>
        public override bool IsConverter(PublishedPropertyType propertyType)
        {
            var propertyEditor = PropertyEditorResolver.Current.GetByAlias(propertyType.PropertyEditorAlias);
            if (propertyEditor == null) return false;
            return propertyEditor.ValueEditor.ValueType.InvariantEquals(PropertyEditorValueTypes.Json);
        }

        public override object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
        {
            if (source == null) return null;
            var sourceString = source.ToString();

            if (sourceString.DetectIsJson())
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject(sourceString);
                    return obj;
                }
                catch (Exception ex)
                {
                    LogHelper.Error<JsonValueConverter>("Could not parse the string " + sourceString + " to a json object", ex);                    
                }
            }

            //it's not json, just return the string
            return sourceString;
        }

        //TODO: Now to convert that to XPath!
    }
}

答案 1 :(得分:0)

您可以尝试编写一些HTML和Razor来获得一些结果。您需要为以下内容安装Bootstrap:

<section class="carousel-wrapper">
    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <div class="carousel-inner" role="listbox">
            @foreach (var slide in CurrentPage.Car)
            {
               if (string.IsNullOrWhiteSpace(slide))
               {
                  continue;
               }

                <div class="item @(slide == CurrentPage.Car[0] ? "active" : string.Empty)">
                    <div class="fullwidth-block full-screen">
                        <img src="@Umbraco.Media(slide.img).Url" class="fill">

                        <div>
                           @slide.desc
                       </div>
                    </div>
                </div>
            }
        </div>
    </div>
</section>

这适用于Umbraco 7.5.13。您可能需要进行一些调试才能在您的方案中使用它。但是根据你的截图判断,你只使用后台吗?不是Visual Studio等。