Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • firepol 125 posts 173 karma points
    Jul 14, 2014 @ 12:27
    firepol
    0

    @Model.MediaById @Model.Media, Library.MediyById not working

    Hi,

    in this tutorial, under Razor Macro I see 3 options to get the url of a picture. None of them work. In Visual Studio when I mouse over "Media" I get the error "Cannot resolve symbol for Media".

    I tried all the methods, I always get this error:

    Error loading Partial View script (file: ~/Views/MacroPartials/NewsList.cshtml).

    In my view at the top I have

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    I tried also:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage

    It never works. Do I forget something, a using statement or so?

    PLEASE HELP

    The only way I can get an url of media is to use the old deprecated way. I'd like to use the new way.

    Old way (works - but as you can see, lot of code just for a little thing tat should work out of the box but I don't know how to make it work):

    var mediaItem = new umbraco.cms.businesslogic.media.Media(Convert.ToInt32(content.GetProperty(propertyName).Value));

    string mediaURL = mediaItem.getProperty("umbracoFile").Value.ToString();

  • Steven Harland 78 posts 518 karma points c-trib
    Jul 15, 2014 @ 10:43
    Steven Harland
    0

    How come you aren't usng the MVC View Example? It uses the UmbracoHelper which I believe is the recommended way to work with content as well as media  in current versions of Umbraco.

  • firepol 125 posts 173 karma points
    Jul 15, 2014 @ 14:54
    firepol
    0

    Dear Steven, if the example would work, I would not be here asking what I'm missing. Also to consider above, I tried the macro example because in my umbraco installation I'm using masterpages and some views have to be inserted as macros, hope you understand...

    I just tried the "view example", in a view i'm using as template for a document type. I have a foreach loop so I can't just use the example as provided in the documentation with "Model.Content" as inside a loop of article, the node would be "article" (and not Model.Content), so I tried like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
        var newsContainer = Model.Content.Children().FirstOrDefault(x => x.DocumentTypeAlias == "NewsContainer");//this has to be like that...
    }
    @foreach (var article in newsContainer.Children.Where(x => x.DocumentTypeAlias == "NewsArticle"))
    {
        @{
            if(article.HasValue("image")){
                var mediaItem = Umbraco.TypedMedia(article.GetPropertyValue("image")); 
                <img src="@mediaItem.Url"/>    
            }   
        }
    } 
    

    What I get is a weird error: "The valueDictionary is not formatted correctly and is missing any of the 'updateDate' elements".

    Of course my article has a proiperty called "image" and it's a media picker and I chose a picture... so the examples I found simply don't work, if you can post some code that is working for you, inside a foreach loop, I'd be glad to test.

    So again, it's impossible for me to get the media url of a picture selected via media picker. I appreciate to get some CODE, complete, also with the first lines with @using and @inherits statements... THX

  • Steven Harland 78 posts 518 karma points c-trib
    Jul 18, 2014 @ 17:07
    Steven Harland
    0

    If you let me know which Umbraco version you're using I can provide you with a complete code example.

  • firepol 125 posts 173 karma points
    Jul 23, 2014 @ 08:34
    firepol
    0

    Hi Steven,

    that would be nice. I'm using umbraco 6.2.1. Thank you.

  • Steven Harland 78 posts 518 karma points c-trib
    Jul 23, 2014 @ 20:50
    Steven Harland
    1

    The following works for me:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
        var newsContainer = Model.Content.Children.FirstOrDefault(x => x.DocumentTypeAlias == "NewsContainer");
    }
    
    @foreach (var article in newsContainer.Children.Where(x => x.DocumentTypeAlias == "NewsArticle"))
    {
        if (article.HasValue("image"))
        {
            var mediaItem = Umbraco.TypedMedia(article.GetPropertyValue("image"));
            <img src="@mediaItem.Url" />
        }
    }
    

    This is pretty much the same as your code except that inside the foreach block I don't try to start a new multi-line statement using "@{" which causes a parser error. I'm not sure how your code runs at all with that in there.

    What line exactly does it fail on?

Please Sign in or register to post replies

Write your reply to:

Draft