2
votes

I'm trying to make my Sitecore site compatible with page editor i.e. all the fields text, rich text editor, etc, should be able to be updated in Page Editor.

So far I've modified the TDS T4 template which returns a HTMLString for each field like below:

public HtmlString HeroImageField(bool isEditable = true, string parameters = "")
{ 
    string renderParameter = GenerateRenderParameter(isEditable, parameters);

    return new HtmlString(Sitecore.Web.UI.WebControls.FieldRenderer.Render(
        Sitecore.Context.Database.GetItem(this.EntityId.ToString()), "Hero Image", renderParameter ));
}

So, in view I just call Model.HeroImage()

Is there a better way to achieve above? Maybe Glass Mapper comes with out of box support for this (which I don't know).

2
So, what is being returned from FieldRenderer in debugger your example? And how are you trying to use that on your view?Martin Miles
Which TDS T4 templates did you edit? Is it these TDS Codegen templates? If so you shouldn't need to edit them, just your usage is not correct like RvanDalen answered.jammykam
@MartinMiles, in view it will be used as Model.HeroImageField(true, "wd=200,ht=300")Nil Pun

2 Answers

3
votes

Sitecore comes with MVC helpers that you can use to make fields editable:

@Html.Sitecore().Field("My Field Name") //Context item by default
@Html.Sitecore().Field("My Field Name", Model.PageItem) //Explicit page item
@Html.Sitecore().Field("My Field Name", Model.Item) //Datasource item (may be context item)

My recommendation is also to use a controller to load a different view for editing. There is often a different visual needed for authoring fields. Carousels/tabs should be flattened out, validation messages need to be exposed, etc.

See more at: http://sitecore-community.github.io/docs/sitecore-mvc/rendering-content/

2
votes

Glass mapper has something OOTB for that. You can use the extended WebViewPage GlassView and use the Editable methods as described here: http://glass.lu/Mapper/Sc/Tutorials/Tutorial14