SaaS CMS has officially launched! Learn more now.

Hide properties of a block used as a property

Vote:
 

I have two blocks which derive from the same class, say

BlockA{
    public virtual string Property1 {get; set;}
    public virtual int Property2 {get; set;}
    public virtual ContentReference Property3 {get; set;}
}

BlockB: BlockA{...}
BlockC: BlockA{
...
   public virtual BlockB PropertyN {get; set;}
}


BlockA has some number of properties. BlockC has a BlockB property which causes that all properties from BlockA are repeated twice in BlockC. Is there any way to hide properties of BlockB, derived from BlockA, in BlockC to avoid the duplicates?

#182172
Edited, Sep 12, 2017 9:21
Vote:
 

Not really, you can hide them in the UI though by overriding them in BlockC and decorate them with [ScaffoldColumn(false)], but not the properties inside BlockB. In this case I wouldn't use inheritance, define what's needed as an interface instead.

#182203
Edited, Sep 12, 2017 12:35
Vote:
 

I don't want to hide the properties from BlockA in BlockC but the ones from BlockB. I couldn't find a solution myself so I created the fields from BlockB in BlockC and populate the BlockB in the controller:

BlockB: BlockA{...}
BlockC: BlockA{
...
   [ScaffoldColumn(false)]
   public virtual BlockB PropertyN {get; set;}
}

public class BlockCController : BlockController<BlockC>{
    
    public override ActionResult Index(BlockC currentContent){
        var clone = (BlockC)currentContent.CreateWritableClone();
        clone.PropertyN = new BlockB
        {
            Property1 = currentContent.BlockBPropertyClone1,
            Property2 = currentContent.BlockBPropertyClone2,
            ....
        };			

        return PartialView(clone);
    }
...
}
#182208
Edited, Sep 12, 2017 13:52
Vote:
 

Creating a writeable clones for this purpose is NOT recommended. Create a view model instead and populate it. Please see https://talk.alfnilsson.se/2016/05/17/how-to-not-use-properties-on-content-types/

Why do you want the same values both in BlockC's and BlockB's properties? That seems strange.

I think you need to re-think your class design here.

#182209
Sep 12, 2017 14:00
Vote:
 

I do NOT want the same properties. I'm working on exisiting solution so re-thinking the class design is quite complicated since there is a lot of content which will be affected by changes.

Basically the customer wants me to add a BlockB property to BlockC in order to avoid creating a new block everytime they create a BlockC. Unfortunately both blocks derive from the same class... Another thing which complicates the situation is that the BlockB has a number of display options and relevant views.

Populating a view model is also an option but then I won't be able to reuse the existing views of BlockB for display options and will need to create such ones for BlockC. My idea was to make use of @Html.RenderContentData in BlockC's view so it takes care of resolving the correct view for chosen display option.

#182211
Edited, Sep 12, 2017 14:09
Vote:
 

I know you don't want the same properties :) That's why I wrote same values. You're setting the same value in multiple properties.

You're just adding code smell. Create a new block type with same properties but WITHOUT inheriting the same base class. You can ensure they have the same properties by defining them in an interface. You can still re-use the same views.

#182212
Sep 12, 2017 14:29
Vote:
 

I don't want the same values either:) Anyway, I will try with view model if I can get that working.

#182213
Sep 12, 2017 14:33
Vote:
 

In your code example currentContent.Property1 and currentContent.PropertyN.Property1 will have the same value, maybe that was a typo? Anyway, a year down the line people will scratch their heads why there is this weird inheritance and why you're assigning the values like this in an immutable (that you mutate with .WritableClone()) object.

#182214
Sep 12, 2017 14:39
Vote:
 

Not really, I have updated my post with correct code. 

Yes, they will do, exactly like I'm sitting now and scratching my head wink

#182215
Sep 12, 2017 14:48
Vote:
 

By the way, I managed to implement it using view model :)

#182216
Sep 12, 2017 15:10
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.