I have a mp3 file that is stored in the media library. It is referenced in a media field on an item. I can get the URL by doing Sitecore.Resources.Media.MediaManager.GetMediaURL() in webforms or a class. But how can I achieve this in a MVC view? When I dump the field it just renders a html tag. I want to pull out the url only.
Also, is there a reference for something like this?
Thanks
You can write an extension method to get the media item URL. Something like this will work (you should add null reference checks btw):
In your cshtml files you can then use:
You need to parse the Raw value of Media or Field using
HtmlString
before using it in Rendering when using withFieldRenderer
.Though if you can provide more code then it can be answered more accurately because in Sitecore there are many ways (Sitecore ORMs and/or APIs) using which you can render Item Fields.
Also refer this Sitecore MVC project on GitHub and below listed Sitecore MVC videos on YouTube it can prove helpful in long run.
And there few blog articles by Martina they are good resource on Sitecore MVC parts.
I hope that will be helpful.
Please ask if anything else.
you have to supply it with the Model that your sending back to your view from the controller:
something like :
Model:
Controller:
View:
regards