1

Controller:

public class RotatorController : SitecoreController 
{
  public ActionResult Index()
  {
    var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
    var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);

    var viewModel = new RotatorViewModel
    {
      RotatorItems = dataSource.Children
    };
    return View(viewModel);
  }
}

View Model:

public class RotatorViewModel
{
  public IEnumerable<Item> RotatorItems { get; set; }
}

View:

@model RotatorViewModel 

<h1>Products List</h1>
@foreach (var item in Model.RotatorItems)
{
  <h2>@item.Fields["Title"]</h2>
      @item.Fields["Image"]
      @item.Fields["Description"]
}

my doubt is:

the description(discription is a field) is of type table....but in webpage it is renderd as tags and also for the image enter image description here

can anyone help me with this?

enter image description here

6
  • Did you try @html.Raw(@item.Fields["Description"])? Feb 7 '17 at 6:58
  • thank you @BalajiKuppuswamy but still the images are not getting rendered Feb 7 '17 at 7:10
  • Did you try with : @Html.Sitecore().Field("nameoffield",@item) ?What is the type of your fields? Feb 7 '17 at 7:12
  • What do you hardcode? Feb 7 '17 at 7:16
  • @SitecoreClimber thank you now its working Feb 7 '17 at 7:44
3

I suggest you use Sitecore's MVC equivalent of a FieldRenderer (more information can be found here: http://sitecore-community.github.io/docs/sitecore-mvc/rendering-content/)

@model RotatorViewModel 

<h1>Products List</h1>
@foreach (var item in Model.RotatorItems)
{
  <h2>@Html.Sitecore().Field("Title", item)</h2>
      @Html.Sitecore().Field("Image", item)
      @Html.Sitecore().Field("Description", item)
}

This will make sure that all your fields output the proper HTML tags. I advise against outputting it manually, even using @Html.Raw(item.Fields["Description"]) for the html formatted content as you're not expanding links in this case.

0

We can use :

@model RotatorViewModel 

<h1>Products List</h1>
@foreach (var item in Model.RotatorItems)
{
  <h2>@item.Fields["Title"]</h2>
      @item.Fields["Image"]
      @item.Fields["Description"]
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.