Copied to clipboard

Flag this post as spam?

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


  • Hemant 45 posts 66 karma points
    Mar 28, 2012 @ 13:12
    Hemant
    1

    Get a media url and name with Razor

    Hi all,

    In my umbraco project with version 3.0.5 ,
    i have used xslt file to display image in header with below code :

     <img alt="">
      <xsl:attribute name="src">

      <xsl:value-of select="umbraco.library:GetMedia($imgnr, 'false')/data[@alias= 'umbracoFile']"/>

      </xsl:attribute>

      </img>

    And , i have upgrade it with version 4.7.1.
    And, now i want to display that header image with RAZOR.

    so, please help me , the equievalent code of above in RAZOR.

    Thanks in advance.

  • Grant Thomas 291 posts 324 karma points
    Mar 28, 2012 @ 13:59
    Grant Thomas
    1

    I don't know where you're getting your media identifier from, but you can do something like this:

    var media = Model.MediaById(yourIdentifier);
    <img src="@media.umbracoFile"/>
  • Tom Fulton 2030 posts 4998 karma points c-trib
    Mar 28, 2012 @ 14:07
    Tom Fulton
    2

    Hi,

    You can use Library.MediaById(mediaId) to get a media item by it's ID:

    @{
      var media = Library.MediaById(Model.myMedia);
      <img src="@media.umbracoFile"/>
    }

    If you need some more Razor resources, check out:

    - Razor Snippets (Wiki)
    - Razor Cheat Sheet
    - Cultiv Razor Examples
    - Razor Estate Agents Starter Site

    HTH,
    Tom

  • Hemant 45 posts 66 karma points
    Mar 28, 2012 @ 14:07
    Hemant
    0

    Hi Grant,

    Thanks for quick reply.

    Here is my whole old xslt  file :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:Stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library"
      exclude-result-prefixes="msxml umbraco.library">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->

    <xsl:variable name="imgnr">
    <xsl:choose>
      <xsl:when test="umbraco.library:GetItem('topbillede') &gt; 0" >
      <xsl:value-of select="umbraco.library:GetItem('topbillede')" />
      </xsl:when>
      <xsl:otherwise>
      <xsl:value-of select="1064" /> <!-- standardbillede -->
      </xsl:otherwise>
    </xsl:choose>
    </xsl:variable>

      <img alt="">
      <xsl:attribute name="src">

      <xsl:value-of select="umbraco.library:GetMedia($imgnr, 'false')/data[@alias= 'umbracoFile']"/>

      </xsl:attribute>

      </img>



    </xsl:template>

    </xsl:stylesheet> 

     

    Please, have a look.
    you can get better idea to solve my problem.

    Thanks. 

  • Grant Thomas 291 posts 324 karma points
    Mar 28, 2012 @ 14:17
    Grant Thomas
    0

    You could do something like:

    var mediaId = Model.topbillede != "0" ? Model.topbillede : "1064";

    Assuming 'topbillede' is part of your Model (or, current document type) and is a string (you can easily adjust to handle integers instead). Otherwise, if it's on a parent or something you could try search recusively with 'Model._topbillede'. Then just as I showed above:

    var media = Model.MediaById(mediaId);
    <img src="@media.umbracoFile"/> 
    Note: the fact that a forum website dedicated to a web-based CMS can't handle editing HTML input is disgraceful. If it wasn't that I noticed so many people needing help in this huge gap... I should have less industry ethics, I guess.
  • Hemant 45 posts 66 karma points
    Mar 28, 2012 @ 14:27
    Hemant
    0

    Hi Tom,

    thanks.
    But, i get nothing , when i use as you told.

     

    Thanks. 

  • Sean Dooley 288 posts 527 karma points
    Mar 28, 2012 @ 17:47
    Sean Dooley
    0

    Maybe something like

    var media = Model.HasValue("topbillede") ? Library.MediaById(Model.Topbillede) : null;
    if(media != null) {
    <img src="@media.Url"/>
  • Hemant 45 posts 66 karma points
    Mar 30, 2012 @ 14:47
    Hemant
    0

    Hi All,

    thanks a lot for your genuine help.

    That works now.

    Thanks. 

  • Hemant 45 posts 66 karma points
    Jul 03, 2012 @ 09:15
    Hemant
    0

    Hi all,

    Last project was completed with success.

    Now, working on another umbraco project.

    but, getting some problem to get image file.

     

    i used :

    @{
     
    var media =Library.MediaById(Model.myMedia);
     
    <img src="@media.umbracoFile"/>}

    Same as in precious one.

    In previous project, i got the media url like : /media/12/something.jpg (that is working fine. http://studenterhusfonden.dk/)

    now, in current project, i am getting the media url : ~/media/12/something.jpg. That is not displaying the image, if i remove "~" from this url working fine.

     

    Thanks.
     


     

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 03, 2012 @ 23:18
    Kasper Dyrvig
    0

    Are you still working in 4.7.1?

  • Hemant 45 posts 66 karma points
    Jul 04, 2012 @ 06:18
    Hemant
    0

    Hi,

    No KD. :)

    That was previous project about 2 months before.

    Now i am working in 4.7.2.

    Thaks.

     

  • Kasper Dyrvig 246 posts 379 karma points
    Jul 05, 2012 @ 23:02
    Kasper Dyrvig
    0

    Sorry for my late respose. I can't see that you are doing anything wrong.

    In my latest project - witch is also 4.7.2 - I'm looping throug a media folder and view every image like this:

    @foreach(var lightboxItem in mediaFolder.Children.Where("NodeTypeAlias == \"Image\"").Where("Visible").Skip(1))
    {
     <href="@lightboxItem.umbracoFile" rel="lightbox[@Model.UrlName]" title="@lightboxItem.umbracoFileAlt"></a>
    }

    And there are no problems. Sorry I could't help more.

  • Hemant 45 posts 66 karma points
    Jul 06, 2012 @ 06:11
    Hemant
    0

    Hi KD,

    I will follow this syntax.

    and np.

    thanks for your help. :) 

  • Hemant 45 posts 66 karma points
    Jul 06, 2012 @ 06:22
    Hemant
    0

    Hi KD,

    When i create scripting file with this above code.

    It not saving the file. 

     

    The Code :

    @foreach(var lightboxItem in mediaFolder.Children.Where("NodeTypeAlias == \"Contact Persons\"").Where("Visible").Skip(1))
    {
     <href="@lightboxItem.umbracoFile" rel="lightbox[@Model.UrlName]" title="@lightboxItem.umbracoFileAlt"></a>
    } 

    The error  :

    d:\Umbraco\ZDesign\macroScripts\634771649617052957_Testforimages.cshtml(2): error CS0103: The name 'mediaFolder' does not exist in the current context  

     

    Thanks.

  • Kasper Dyrvig 246 posts 379 karma points
    Aug 07, 2012 @ 13:50
    Kasper Dyrvig
    0

    Arh yes... before the loop you must create a var called mediaFolder:

    var mediaFolder Library.MediaById(Model.whatEverYouHaveNamedTheParameter);
    
  • Hemant 45 posts 66 karma points
    Aug 22, 2012 @ 14:06
    Hemant
    0

    Thanks all. :)

     

    It's working now.

     

     

Please Sign in or register to post replies

Write your reply to:

Draft