Xafari MVC. Property and List Editors

To implement a new Property Editor or List Editor, follow the rules described in the next sections: How to: Implement a Property Editor Based on Custom Controls (ASP.NET) and How to: Implement an ASP.NET Web List Editor Using a Custom Control.

It is required to satisfy the following conditions:

First of all, the editor class should implement the IMvcControl interface that is used instead of a WebForm control. That is why the editor can be considered both a WebForm control and a "Model" (in terms of MVC concept only).

IMvcControl provides the TemplatePartialView property that specifies the path to the corresponding .cshtml file which stores the source code for the control. There are three possible ways to implement the TemplatePartialView property:

  1. Specify the path explicitly. For instance:
  • c#
  • VB

public class MVCxCustomListEditor : ListEditor, IMvcControl, IComplexListEditor
{
  //...
  public MVCxCustomListEditor(IModelListView model)
    : base(model)
  {
    //...
    this.TemplatePartialView = "Editors/Xafari.Editors.Mvc/MvcTabbedDetailPropertyEditor/_Editor";
  }
  //...
}

Public Class MVCxCustomListEditor
  Inherits ListEditor
  Implements IMvcControl, IComplexListEditor
  '...
  Public Sub New(ByVal model As IModelListView)
    MyBase.New(model)
    '...
    Me.TemplatePartialView = "Editors/Xafari.Editors.Mvc/MvcTabbedDetailPropertyEditor/_Editor"
  End Sub
  '...
End Class

  1. Use PartialViewTemplateAttribute on the editor class and specify the path to PartialView as an attribute parameter. Alternatively, use one of the constants of the class to generate the path based on editor properties.
  2. Set the PartialViewTemplate property in XAF Application Model.

The second and third cases allow the ObjectFormmater syntax:

  • c#
  • VB

public class MVCxCustomListEditor : ListEditor, IMvcControl, IComplexListEditor
{
  //...
  public virtual string TemplateFolder
  {
    get
    {
      return ObjectFormatter.Format("Editors/{0:Namespace}/{0:Name}", this.GetType());
    }
  }
  //...
  public MVCxCustomListEditor(IModelListView model)
    : base(model)
  {
    //...
    this.TemplatePartialView = string.Format("{0}/_Editor", this.TemplateFolder);
  }
  //...
}

Public Class MVCxCustomListEditor
  Inherits ListEditor
  Implements IMvcControl, IComplexListEditor
  '...
  Public Overridable ReadOnly Property TemplateFolder As String
    Get
      Return ObjectFormatter.Format("Editors/{0:Namespace}/{0:Name}", Me.[GetType]())
    End Get
  End Property
  '...
  Public Sub New(ByVal model As IModelListView)
    MyBase.New(model)
    '...
    Me.TemplatePartialView = String.Format("{0}/_Editor", Me.TemplateFolder)
  End Sub
  '...
End Class

The next condition is that the CreateControlCore method should include the following code:

  • example

return new AdapterMvcControl<Editor type>(this);

The presence of this string is obligatory because the editor should be a "Model" (in MVC terms). Moreover, the XAF logic for using editors should never dispose the editor object.

To see custom editors, refer to the Featore Center demo installed with Xafari MVC.

Finally, each editor should have a .cshtml file that contains the HTML code for a particular editor.