5

I have a model ArticleItem which has some fields that I populate with some Business Logic.

Now on the view where I return ArticleItem I want to render a RTE content so that it is Page Editor friendly

@Html.Sitecore().Field("Article Body", @Model.ArticleItem)

is not working.

Does it only works with Sitecore.Items.Item?

If I want to use a custom Model instead how can I render RTE so it is Page Editor mode friendly?

I cannot use the context item in this case as I am rendering from a multilist inside the Context Item.

5

@Html.Sitecore().Field only works with real items and it sounds like Model.ArticleItem is not a real Sitecore Item.

What you want to do is render the Sitecore field as HTML in your model and render that.

// get your sitecore item
var item = RenderingContext.Current.ContextItem;

// get the sitecore editor html (will only render the data if not in edit mode) 
var fieldHtml = SC.Web.UI.WebControls.FieldRenderer.Render(item, "title");

// bind the fieldHtml to a HtmlString field on your model
MyModel.TitleField = fieldHtml


public class MyModel
{
    public HtmlString TitleField { get; set; }
}

Now in you view you can just render the model's TitleField property and you will get the RTE editor you want in page.

<div>@Model.TitleField</div>
  • Thanks! this work. What do I do for Images and MultList/TreeList any thoughts? – Renuka Apr 29 at 22:54
1

In your controller (or service), you could also add the Sitecore.Items.Item on the view model that you send to your view and then use that item to render the content and Experience Editor functionality:

Controller/service

var contextItem = RenderingContext.Current.ContextItem;

var articleItem = // get article item from multilist on contextItem;

var viewModel = new ArticleViewModel
                    {
                        /// ... other business fields ...
                        ArticleItem = articleItem;
                    };

View

@Html.Sitecore().Field("Article Body", @Model.ArticleItem)
  • this is what I am doing currently but it does not work because apparently it works only with Sitecore Item and not Model Item unless handled with FieldRender.Render – Renuka Apr 30 at 17:14
  • Yes, that's right; articleItem will need to be the actual Sitecore.Items.Item. – Dan Sinclair Apr 30 at 17:14
-2

Views.cshtml:

<div>
  @Html.Sitecore().field(@Model.Article)
</div>

in Model.cs

public string Article
        {
            get { return Templates.Models.Articles.Article;}
        }
  • this does not work. – Renuka Apr 30 at 17:15

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.