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.
@{ 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.
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.
I don't know where you're getting your media identifier from, but you can do something like this:
Hi,
You can use Library.MediaById(mediaId) to get a media item by it's ID:
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
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 " "> ]>
<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') > 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.
You could do something like:
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:
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.Hi Tom,
thanks.
But, i get nothing , when i use as you told.
Thanks.
Maybe something like
Hi All,
thanks a lot for your genuine help.
That works now.
Thanks.
Hi all,
Last project was completed with success.
Now, working on another umbraco project.
but, getting some problem to get image file.
i used :
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.
Are you still working in 4.7.1?
Hi,
No KD. :)
That was previous project about 2 months before.
Now i am working in 4.7.2.
Thaks.
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:
And there are no problems. Sorry I could't help more.
Hi KD,
I will follow this syntax.
and np.
thanks for your help. :)
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))
{
<a 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.
Arh yes... before the loop you must create a var called mediaFolder:
Thanks all. :)
It's working now.
is working on a reply...