logikfabrik

How to model bind the current member in Umbraco 8

3 min read

For this example I created a document type Profile Page, and a controller to go with it. I used the Models Builder mode LiveAppData. I included the generated files App_Data/Models/ProfilePage.generated.cs, and App_Data/Models/Member.generated.cs1 in my VS project. I also added a Member typed parameter to the default controller action Index:

namespace Logikfabrik.Umbraco.Controllers
{
  using System.Web.Mvc;
  using global::Umbraco.Web.Models;
  using global::Umbraco.Web.Mvc;
  using global::Umbraco.Web.PublishedModels;

  public class ProfilePageController : RenderMvcController
  {
    public ActionResult Index(ContentModel<ProfilePage> model, Member member)
    {
      return CurrentTemplate(model);
    }
  }
}

Requests for ProfilePage content are now routed to this action.

The Member model implements IPublishedContent. The default model binder ContentModelBinder binds ContentModel, ContentModel<>, and, IPublishedContent parameters. So ContentModelBinder tries, and fails, to bind ProfilePage content to both the model, and member parameters:

[ModelBindingException: Cannot bind source content type Umbraco.Web.PublishedModels.ProfilePage to model type Umbraco.Web.PublishedModels.Member.]

Add the following to your VS project:

namespace Logikfabrik.Umbraco
{
  using System.Web.Mvc;
  using global::Umbraco.Web.Mvc;
  using global::Umbraco.Web.PublishedModels;

  public class MemberModelBinder : IModelBinder
  {
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      var umbraco = (controllerContext?.Controller as RenderMvcController)?.Umbraco ??
                    (controllerContext?.Controller as SurfaceController)?.Umbraco;

      if (umbraco == null)
      {
        return null;
      }

      var member = umbraco.MembershipHelper.GetCurrentMember();

      return member == null ? null : new Member(member);
    }
  }
}
namespace Logikfabrik.Umbraco
{
  using System;
  using System.Web.Mvc;
  using global::Umbraco.Web.PublishedModels;

  public class MemberModelBinderProvider : IModelBinderProvider
  {
    public IModelBinder GetBinder(Type modelType)
    {
      return modelType == typeof(Member) ? new MemberModelBinder() : null;
    }
  }
}
namespace Logikfabrik.Umbraco
{
  using System.Web.Mvc;
  using global::Umbraco.Core.Composing;

  public class Component : IComponent
  {
    public void Initialize()
    {
      ModelBinderProviders.BinderProviders.Insert(0, new MemberModelBinderProvider());
    }

    public void Terminate()
    {
    }
  }
}
namespace Logikfabrik.Umbraco
{
  using global::Umbraco.Core.Composing;

  public class Composer : ComponentComposer<Component>, IUserComposer
  {
  }
}

Composer and Component adds MemberModelBinderProvider to the list of providers on start-up. MemberModelBinder will bind the current member to Member typed action parameters. In this case the member parameter.

And that’s it.


  1. Model generated for the default member type Member. See the Umbraco documentation.