简体   繁体   English

ASP.Net使用Umbraco CMS(Archetype)在视图中显示图像

[英]ASP.Net Display image in view using Umbraco CMS (Archetype)

So I'm using Umbraco CMS with Archetype and trying to get an image property to display on the frontend. 因此,我将Umbraco CMS与Archetype结合使用,并尝试使image属性显示在前端。 I have it displaying my other 3 fields as per attached image but I'm struggling to show it for the mediaitem, what am I doing wrong 我按照附加的图像显示了我的其他3个字段,但我正努力将其显示给mediaitem,我在做什么错

// HomeController.cs // HomeController.cs

    public class HomeController : SurfaceController {
       public ActionResult RenderTheGroup()
    {
        List<FeaturedItem> model = new List<FeaturedItem>();
        IPublishedContent homepage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();
        ArchetypeModel featuredItems = homepage.GetPropertyValue<ArchetypeModel>("featuredItemList");

        foreach(ArchetypeFieldsetModel fieldset in featuredItems)
        {
            int imageId = fieldset.GetValue<int>("image");
            var mediaItem = Umbraco.Media(imageId);
            string image = mediaItem.Url;               

            model.Add(new FeaturedItem(
                fieldset.GetValue<string>("name"), 
                fieldset.GetValue<string>("paragraph"), 
                fieldset.GetValue<string>("link"),
                image
                ));
        }

        return PartialView(PARTIAL_VIEW_FOLDER + "_TheGroup.cshtml", model);
    }
}

// Model - FeaturedItem.cs //模型-FeaturedItem.cs

    public class FeaturedItem
{
    public string Name { get; set; }
    public string Paragraph { get; set; }
    public string Image { get; set; }
    public string LinkUrl { get; set; }

    public FeaturedItem(string name, string paragraph, string image, string linkUrl)
    {
        Name = name;
        Paragraph = paragraph;
        Image = image;
        LinkUrl = linkUrl;
    }
}

} }

// View //查看

        <div class=" col-md-3">
            <div class="thumbnail">
               @RenderFeaturedItem(@Model[0], null)
            </div>
        </div>
        @helper RenderFeaturedItem(FeaturedItem item, string extraClass)
        {
                <img src="@item.Image" alt="">
                <div class="">
                    <div class="caption">
                        <h4>
                            <a target="_blank" href="@item.LinkUrl">@item.Name</a>
                        </h4>
                        <p>@item.Paragraph</p>
                    </div>
                    <div class="">
                        <p>
                            <a target="_blank" href="@item.LinkUrl">
                                Link here
                                <i class="fa fa-chevron-right" aria-hidden="true"></i>
                            </a>
                        </p>
                    </div>
                </div>
        }

I don't know if it would still be helpful to you but I solved this problem the following way: 我不知道它是否仍然对您有帮助,但是我通过以下方式解决了这个问题:

int imageId = fieldset.ToPublishedContent().GetPropertyValue<int>("image");
var mediaItem = Umbraco.Media(imageId);
string image = mediaItem.Url;    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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