Related Tags:
ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, CMS A content management system (CMS) is a software application or set of related programs that are used to create and manage digital content. CMSes are typically used for enterprise content management (ECM) and web content management (WCM) Learn More, Sitecore Sitecore is one of the leading enterprise-level content management systems built on ASP.NET, enabling web content editors and marketers to have full control over all aspects of their website from social integration and blog posts to advanced personalisation, e-commerce and more. Launched in 2001, Sitecore has used the .NET platform from the beginning of the language itself, and has been growing in popularity over the last few years. Currently on its 7th major version, it now runs on .NET 2.0/4.0, and the core has been rewritten from scratch to take advantage of the improvements made in ASP.NET 4.5. Learn More, Sitecore 9 Sitecore is a Web Content Management System (CMS) built on Microsoft ASP.net. This tag is for sitecore version 9. Learn More, MVC 4 It is a newer version of ASP.NET MVC,ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. Learn More,

How to get image url from a image field in Sitecore 9 at MVC view?

Description:I am using following code chunk which is actually generating complete img tag but I only need image url because I have to set only the url in some CSS property.


@foreach (var item in ViewBag.Items)
{
<li class="slide">
<div class="item featured-item1" style='background-image: url(@Html.Sitecore().Field("Image", item));'>
<div class="content">
<h3>@Html.Sitecore().Field("Title", item)</h3>
<div class="buttons-items">
<a href="@Html.Sitecore().Field("BuyNow", item)" class="btn-buy">BUY NOW</a>
<a href="@Html.Sitecore().Field("LearnMore", item)" class="btn-more">LEARN MORE</a>
</div>
</div>
</div>
</li>
}

In the above code "ViewBag.Items" contains Sitecore items, and field name is "Image" for which I am trying get the image url.

Posted by: | Posted on: Aug 02, 2018

1 answers

Replies

3

Hi Junaid,
I understood your issue, you can get the Image url in sitecore 9 using mvc by the following two line of code.

Sitecore.Data.Fields.ImageField imageField = item.Fields["Image"];
string imageUrl= Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem);

So your updated code will become some like the following one.

@foreach (var item in ViewBag.Items)
{
<li class="slide">
@{Sitecore.Data.Fields.ImageField imageField = item.Fields["Image"];}
<div class="item featured-item1" style='background-image: url(@(Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem)));'>
<div class="content">
<h3>@Html.Sitecore().Field("Title", item)</h3>
<div class="buttons-items">
<a href="@Html.Sitecore().Field("BuyNow", item)" class="btn-buy">BUY NOW</a>
<a href="@Html.Sitecore().Field("LearnMore", item)" class="btn-more">LEARN MORE</a>
</div>
</div>
</div>
</li>
}

Hope this may help you !!!

Replied by: | Replied on: Aug 02, 2018



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview