When using the default siteEditField function

@Html.SiteEditField(Model.Component, Model.Component.Fields["Heading"])

And the field does not exist in the broker it generates an exception, that the field does not exist. What I expected was that the method

public static MvcHtmlString SiteEditField(this HtmlHelper helper, IComponent component, IField field, int index)

Would validate the input of value, if the field is empty or none existing to generate the siteedit tags for empty field.

How can this best be achieved; Extending the SiteEditHelper is not allowed since its a sealed class.

  • which version of DD4T are you using? Sep 4 '14 at 15:29

You could create your own SiteEdit Extension methods helper which wrap the DD4T one e.g.

 public static class MySiteEditHelpers
      public static MvcHtmlString SafeSiteEditField(this HtmlHelper helper, IComponent component, IField field)
           if(field != null)
              return helper.SiteEditField(...)
           return new MvcHtmlString(string.Empty);

(Alternatively || Additionally) you could raise this as an issue on the DD4T GitHub, fork the repo, fix it, and submit a pull request for the next release!

Your Answer

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