Cant get the image to show in Umbraco7 with razor

老子叫甜甜 提交于 2019-12-13 05:45:13

问题


I have used the media picker as data type for the type, for which the user is going to choose what image they want as the deal image.

But for some reason i can't get the razor syntax to show the image. If I make an If statement to check if the page contains an image then it won't. I think this is a problem that occurs because i have misunderstood something.

My current razor statement:

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

The above code won't show anything.

Hope any of you can guide me to what i do wrong and evt. stuff I'm missing.

This is how my current home.cshtml looks like:

 @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>

回答1:


You need to use Umbraco.Media to get the media. So like this

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

Or

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



回答2:


An example of using Umbraco.Media:

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

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



回答3:


Link on OUR Umbraco offers two solutions:

Typed:

@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" />      
        }                                                               
}

Dynamic:

@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" />
    }
}

Also, double check your media picker data type alias. Typos are rather common in this part.




回答4:


This may be a bit clunky compared to other answers but this is what I currently have.

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


来源:https://stackoverflow.com/questions/28301757/cant-get-the-image-to-show-in-umbraco7-with-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!