Skip to main content
edited tags
Link
Mark Cassidy
  • 25.4k
  • 7
  • 59
  • 117
Minor edits, added mvc tag
Source Link
Jason St-Cyr
  • 6.8k
  • 1
  • 19
  • 46

How to retireveretrieve image url in mvcMVC view from model

I am new to sitecoreSitecore. I am trying to create a small demo which will display all the products with its image and description.
But i am getting error in my view also i am not able to display anything.

How to retireve image url in mvc view from model

I am new to sitecore. I am trying to create a small demo which will display all the products with its image and description.
But i am getting error in my view also i am not able to display anything.

How to retrieve image url in MVC view from model

I am new to Sitecore. I am trying to create a small demo which will display all the products with its image and description.
But i am getting error in my view also i am not able to display anything.

Source Link

How to retireve image url in mvc view from model

I am new to sitecore. I am trying to create a small demo which will display all the products with its image and description.
But i am getting error in my view also i am not able to display anything.

here is my code:
ProductController

        using MicrosoftDemo.Models;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web.Mvc;
        using Sitecore.Data.Fields;
        using Sitecore.Resources.Media;

        namespace MicrosoftDemo.Controllers
       {
       public class ProductController : Controller
       {
        // GET: Product
        public ActionResult Index()
        {
            var context = Sitecore.Context.Item;

            var products = context
                .GetChildren()
                .Where(x => x.TemplateID
                                .ToString() == "{6BDC57DE-9C93-4922-9GA8- 
                                      049J387ACBE2}");

            var productModelList = new List<ProductModel>();

            if (products != null && products.Any())
            {
                foreach (var product in products)
                {
                    if (product == null)
                        continue;

                    var imgField = (ImageField)product
                        .Fields["ProductImage"];

                    var items = new ProductModel
                    {
                        ProductImage = MediaManager
                            .GetMediaUrl(imgField.MediaItem),
                        ProductName = product
                            .Fields["ProductName"]
                            .Value,
                        ProductDescription = product
                            .Fields["ProductDescription"]
                            .Value
                    };
                    productModelList.Add(items);
                }
            }
            return View(productModelList);
        }
    }
}

Index.cshtml

 @model IEnumerable<MicrosoftDemo.Models.ProductModel>
@using Sitecore.Mvc;


@if (Model.Any())
{
    <br/><br/>
    <div class="row">
        <div class="col-md-10">
            <div class="row">
                @foreach (var item in Model)
                {
//                    how do i print multiple items am i doing anything wrong here [email protected]().Field not displaying anything
                    <div class="col-md-5">
                        <h3>
                            <span>
                                @*I am getting error below cannot convert from string to SiteCore.Data.Items.Item*@
                                @Html.Sitecore().Field("ProductImage", item.ProductImage, new {@class = "img-responsive"})
                            </span>
                            @Html.Sitecore().Field("ProductName", item.ProductName)
                        </h3>
                        <br/>
                        <p class="myfont">@Html.Sitecore().Field("ProductDescription", item.ProductDescription)</p>
                    </div>
                }
            </div>
        </div>
    </div>
}

Can anybody tell me how to achieve this ? Any help would be much appreciated