Rob Allen
Rob Allen

Reputation: 2911

reusing view with multiple models in Sitecore MVC using glassmapper

I'm having trouble determining how I might reuse views in my sitecore 8 solution using glassmapper. I have two object that contain the same data, but the fields are named differently depending on the template. Title vs Heading, for instance.

This is relatively simple when the page editor isn't a factor as I can just map/cast one object to another, but this seems to choke up the page editor.

The most obvious path is to use Interfaces and extension methods as described here, but this requires creating a real kitchen sink of an Interface.

Any other suggestions? I'd hate to copy the view just to change what properties it uses.

Upvotes: 2

Views: 862

Answers (3)

Martin Miles
Martin Miles

Reputation: 2096

Agree to what suggested by jammykam in his comment above - the most straightforward would be to rename template fields to be the same.

So, as per your example they will be all physically called Title; for content editors you may use help display name for particular field, so the will still see them as Title vs. Heading, despite in fact they both are called Title for the system. Of course, if your solution can allow renaming like that... More about this approach by the link: http://goo.gl/b28s2y (last part).

Another option to consider - you may think about proxy objects on top of your glass models that would re-map your field names to whatever you want. Thus passing your proxy objects having same names to a view.

But, right you are, in hardest case probably interfaces would be an answer

Upvotes: 1

jammykam
jammykam

Reputation: 16990

If you are using Controller Renderings then you could define an interface like so:

public interface ITitleAndBody : IGlassBase 
{
    string Title  {get; set;}
    string Body  {get; set;}
}

And concrete types like so:

public class MyType1 : GlassBase, ITitleAndBody 
{
    [SitecoreField("Title")]
    public virtual int Title  {get; set;}

    [SitecoreField("Body")]
    public virtual string Body  {get; set;}
}

public class MyType2 : MyType1
{
    [SitecoreField("Header")]
    public override int Title  {get; set;}
}

You will need two controller actions pointing to the same view:

public ActionResult MyType1()
{
    return PartialView("/Views/MyView.cshtml", SitecoreContext.GetCurrentItem<MyType1>());
}

public ActionResult MyType2()
{
    return PartialView("/Views/MyView.cshtml", SitecoreContext.GetCurrentItem<MyType2>());
}

And in the view itself use the interface as the model:

@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<ITitleAndBody>

<div>@Editable(Model, x => x.Title)</div>
<p>@Editable(Model, x => x.Body)</p>

This is untested code. It would have been nicer to use View Renderings of course.

Upvotes: 4

xoail
xoail

Reputation: 3064

Alternately, if the fields are same, you can change their titles on template's field properties. That way when I create an item of template X I would be presented with Field's title assigned on template X while the FieldName itself remains same.

But if I have to do it, I would prefer to use Interfaces with Glass.

Upvotes: 0

Related Questions