我正在使用CurrentPage.GetPropertyValue(" promo")但是每个不循环使用了
@{
var SiteLinks = CurrentPage.GetPropertyValue("promo");
if (CurrentPage.HasValue("promo"))
{
var promoListValue = CurrentPage.GetPropertyValue("promo");
foreach (var item in promoListValue)
{
<span>@item.alias </span>
}
}
}
我收到错误但是当我打印promoListValue时它会显示如下
[ { "alias": "1", "content": "1", "img": "/media/1069/509253678.jpg" }, { "alias": "Slide 2", "content": "2", "img": "/media/1074/636609180.jpg" } ]
有人可以帮助我:)
即使我已经创建了一个属性值转换器,所以代码就像这样
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.HomePage>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@using System.Web.Script.Serialization;
@using Newtonsoft.Json.Linq;
@using System.Collections.Generic;
@using Umbraco.Core.Models.PublishedContent;
@using Umbraco.Core.PropertyEditors;
@using Newtonsoft.Json;
@using System.Linq;
@{
Layout = "Master1.cshtml";
}
@{
if (CurrentPage.HasValue("promo"))
{
var promoListValue = CurrentPage.GetPropertyValue("promo");
@* var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(promoListValue);
var testmodel = serializer.DeserializeObject(json);
foreach (var item in promoListValue)
{
<span>@item </span>
}
@Html.Raw(json)
@Html.Raw(Json.Encode(json))
@Html.Raw(testmodel) *@
var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("promo");
<span>@obj </span>
}
}
<div class="container _borderless landingpage">
<div class="row">
<div class="col-md-4 p0"><a href="/news">
@Html.Partial("NewsCarousel")
<!-- Main nav ends -->
</a></div>
<div class="col-md-8 p0">
<article class="testimonial-feed p0">
</article>
<div id="test3"></div>
</div>
</div>
</div>
@functions {
public class MyLookAlikeArchetypeFieldSet
{
[JsonProperty("alias")]
public string alias { get; set; }
[JsonProperty("img")]
public string img { get; set; }
}
public class MyLookAlikeArchetypeModel
{
private List<MyLookAlikeArchetypeFieldSet> _Items;
public MyLookAlikeArchetypeModel()
{
_Items = new List<MyLookAlikeArchetypeFieldSet>();
}
public MyLookAlikeArchetypeModel(List<MyLookAlikeArchetypeFieldSet> list)
{
_Items = list;
}
public IEnumerator<MyLookAlikeArchetypeFieldSet> GetEnumerator()
{
return _Items.GetEnumerator();
}
public bool Any()
{
return _Items.Any();
}
}
public class MyLookAlikeArchetypeModelPropertyConverter : IPropertyValueConverter
{
public object ConvertDataToSource(PublishedPropertyType propertyType, object source, bool preview)
{
return source;
}
public object ConvertSourceToObject(PublishedPropertyType propertyType, object source, bool preview)
{
try
{
var list = JsonConvert.DeserializeObject<List<MyLookAlikeArchetypeFieldSet>>(source as string);
return new MyLookAlikeArchetypeModel(list);
}
catch
{
return new MyLookAlikeArchetypeModel();
}
}
public object ConvertSourceToXPath(PublishedPropertyType propertyType, object source, bool preview)
{
return null;
}
public bool IsConverter(PublishedPropertyType propertyType)
{
return propertyType.PropertyEditorAlias.Equals("My.Plugin.Package.Manifest.Alias");
}
}
}
但是var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("promo");
obj正空着