这是我的代码
@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";
}
<!-- Top menu -->
@*@Html.Partial("TopMenu")*@
<!-- End top menu -->
<!-- Main nav -->
@Html.Partial("MainMenu")
<!-- Main nav ends -->
@Html.Partial("PromoCarousel")
<div class="landing-page">
@CurrentPage.GetGridHtml("bodyText")
</div>
<div class="col-lg-12">
@Umbraco.Field("promo")
</div>
@{
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>
foreach(var item in obj)
{
<p>@item.img</p>
<p>@item.alias</p>
}
}
}
<div class="container _borderless landingpage">
</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");
}
}
}
但obj返回为空
var obj = Model.Content.GetPropertyValue<MyLookAlikeArchetypeModel>("promo");
CurrentPage.GetPropertyValue("promo");
[ { "alias": "1", "content": "1", "img": "/media/1069/509253678.jpg" }, { "alias": "Slide 2", "content": "2", "img": "/media/1074/636609180.jpg" } ]
任何帮助将不胜感激
答案 0 :(得分:0)
为什么不对它进行反序列化。
这样的事情......(抱歉没有测试过这个)
var object = JsonConvert.DeserializeObject<MyLookAlikeArchetypeModel> (archetypeValueAsString);
foreach (var item in object)
{
var Img = item.GetValue<string>("img");
}