1

I was wondering if there was something more modern than having to create a Model item under /sitecore/layout/Models for every view rendering I have.

If I'm informing the view the namespace and type I want as Model, why can't the view infer automatically somehow this information and auto-populate my model with the right fields?

2 Answers 2

3

It's not bad practice as such, but for efficiency we can do away with the need to create a Model definition item in Sitecore, and determine the Model from the View itself.

I recently wrote a blog post on this very subject as I was curious, but to summarise:

John West prototyped a pipeline processor to do exactly this back in 2014.

He proposed patching a processor into the mvc.getModel pipeline to inspect the compiled view type and return an instance of the custom Model class (should it be set).

Snippet from the processor (summarised for brevity):

public override void Process(GetModelArgs args)
{
    Type compiledViewType = BuildManager.GetCompiledType(path);
    Type baseType = compiledViewType.BaseType;

    if (baseType == null || !baseType.IsGenericType)
    {
        // Log error
    }

    var modelType = baseType.GetGenericArguments()[0];
    if (modelType == typeof(object))
    {
        // When no @model is set, the result is a ViewPage<object>
        throw new Exception(string.Format(
            "View '{0}' needs a @model directive.",
            args.Rendering.RenderingItem.InnerItem["path"]));
    }

    args.Result = Activator.CreateInstance(modelType);
}

A couple of years later Nat Mann proceeded to run with John's idea, taking the same fundamental approach, but optimised the prototype for performance and fixed an issue for layouts without a model specified. Nat's version can be found here here.

Glass Mapper has also incorporated code into it's popular library to get the Model from the View as of release 4.0.0.4.

2
  • Amazing, thanks for the answer. Is there a known reason why this approach was not officially incorporated by the product? Commented Feb 5, 2019 at 15:05
  • 1
    @HugoSantos Sitecore is heavily supported, customised and extended by the community and this is just one of hundreds, (possibly thousands) of community led tweaks and additions. Sitecore tends to leave it to the community, whether intentionally or not to fill in the 'gaps' as we see it. Having said that, from time to time, they do incorporate heavily requested/desirable features, for example Dynamic placeholders and role based configuration. As to what exactly Sitecore chooses to incorporate into the product and when, we can only speculate.
    – sitecorium
    Commented Feb 5, 2019 at 15:42
0

I would recommend using a Controller Rendering instead of a View Rendering for a few reasons:

  1. You do not need to specify a model type in /sitecore/layout/Models.
  2. In general, when you have a rendering with a data source (model) it should use a Controller Rendering so that you can fail gracefully if a user does not specify a data source item. (For View Renderings, a missing data source means a 500 error.)
  3. With a Controller Rendering, you'll have more control over initializing your model and if you want to add more data to it in the future you'll have that flexibility.
2
  • Those points make entirely sense. But what about the performance difference between both options? Commented Feb 5, 2019 at 15:02
  • I haven't profiled both methods, but I suspect (anecdotally) that the performance impact is minimal, especially if presentation caching is enabled and configured Commented Feb 5, 2019 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.