【问题标题】:Cant get the image to show in Umbraco7 with razor无法使用剃刀在 Umbraco 7 中显示图像
【发布时间】:2015-04-02 19:36:09
【问题描述】:

我使用媒体选择器作为类型的数据类型,用户将选择他们想要的图像作为交易图像。

但由于某种原因,我无法使用剃刀语法来显示图像。如果我做一个 If 语句来检查页面是否包含图像,那么它不会。我认为这是一个问题,因为我误解了一些东西。

我目前的剃须刀声明:

<img src="@Umbraco.TypedMedia(Model.Content.GetPropertyValue("deal1image")).Url" />

上面的代码不会显示任何东西。

希望你们中的任何人都可以指导我做错了什么和 evt。我缺少的东西。

这是我当前 home.cshtml 的样子:

 @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "Master.cshtml";
}
<div class="container">
    <div class="col-md-4">
        <!--Image here-->
        <img src="@Umbraco.Media(CurrentPage.deal1image).Url" />


        <div class="thumbnail thumbnailcustom thumbnailbg1">
            <h3>@Umbraco.Field("dealtitle1")</h3>
            <p>@Umbraco.Field("dealdescription1")</p>
        </div>
    </div>
    <div class="col-md-4">
        <!--Image here-->
        <div class="thumbnail thumbnailcustom thumbnailbg2">
            <h3>@Umbraco.Field("dealtitle2")</h3>
            <p>@Umbraco.Field("dealdescription2")</p>
        </div>
    </div>
    <div class="col-md-4">
        <!--Image here-->
        <div class="thumbnail thumbnailcustom thumbnailbg3">
            <h3>@Umbraco.Field("dealtitle3")</h3>
            <p>@Umbraco.Field("dealdescription3")</p>
        </div>
    </div>
</div>

【问题讨论】:

  • 您是否在调试时检查了 Model.Content.GetPropertyValue("deal1image") 返回的内容?而且如果@Umbraco.TypedMedia(Model.Content.GetPropertyValue("deal1image")).Url 确实没有返回空字符串?
  • 另外,我不确定您是否应该使用 Umbraco.TypedMedia。至少我从来没有这样做过,我使用 Umbraco.Media。
  • 我只是试图检查 value 是否为 null 。在 Model.Content.GetPropertyValue("deal1image") 部分中,“deal1image”是数据类型别名对吗?
  • 你会如何使用@Umbraco.Media?
  • 您传递给 GetPropertyValue 的内容应该是所需的 documentType 属性的别名

标签: asp.net asp.net-mvc razor umbraco umbraco7


【解决方案1】:

您需要使用Umbraco.Media 获取媒体。就这样

<img src="@Umbraco.Media(Model.Content.GetPropertyValue("deal1image").ToString()).Url" />

或者

<img src="@Umbraco.Media(CurrentPage.deal1image).Url" />

【讨论】:

  • @McBoman 当前页面是否包含别名为“deal1image”的媒体字段?如果是这样,请尝试编写 @CurrentPage.deal1image 并查看它是否返回 Id。上面的代码是获取媒体项的正确方法。
  • 我不认为我在文档类型中以错误的方式设置了媒体。
【解决方案2】:

使用 Umbraco.Media 的示例:

var myPage = CurrentPage.AncestorsOrSelf().Where("DocumentTypeAlias == @0", "yourPageAlias").First();

Umbraco.Media(myPage.myImage.ToString()).Url

【讨论】:

  • 很好的答案附有代码示例,并为未来的读者提供了解释。虽然问这个问题的人可能理解你的答案,但解释你是如何得出这个问题的可能会帮助无数其他人。
【解决方案3】:
OUR Umbraco 上的

Link 提供两种解决方案:

键入:

@if (Model.Content.HasValue("caseStudyImages"))
{
    var caseStudyImagesList = Model.Content.GetPropertyValue<string>("caseStudyImages").Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
    var caseStudyImagesCollection = Umbraco.TypedMedia(caseStudyImagesList).Where(x => x != null);

    foreach (var caseStudyImage in caseStudyImagesCollection)
        {      
            <img src="@caseStudyImage.Url" style="width:300px;height:300px" />      
        }                                                               
}

动态:

@if (CurrentPage.HasValue("caseStudyImages"))
{
    var caseStudyImagesList = CurrentPage.CaseStudyImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    var caseStudyImagesCollection = Umbraco.Media(caseStudyImagesList);

    foreach (var caseStudyImage in caseStudyImagesCollection)
    {
        <img src="@caseStudyImage.Url" style="width:300px;height:300px" />
    }
}

另外,请仔细检查您的媒体选择器数据类型别名。这部分错别字比较常见。

【讨论】:

    【解决方案4】:

    与其他答案相比,这可能有点笨拙,但这是我目前拥有的。

    var imageId = Model.Content.GetPropertyValue<int>("eventPoster"); // gets node id
    var evId = evpId.Id; // gets image id
    var evMd = Umbraco.Media(evId); // I believe this turns the id into a string
    var evUrl = evMd.Url; // gets the url of the string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多