Search code examples
orchardcms

Pass the current Model down to a more specific shape


TL;DR: How do I pass the model down into the next shape?

I have modified the Orchard.Search modules search results view in my theme (Views\Orchard.Search\Search\Index.cshtml), and split it out into several scenarios. I test for one result and just redirect straight through and test for no search terms and multiple search search results.

I would like to put these scenarios in separate .cshtml views but these specialised shapes need access to the main search result model (@model Orchard.Search.ViewModels.SearchViewModel).

I tried using Arguments.From to make a dynamic object which works but then I have to cast everything in the view back to its correct data type because the next shape down thinks every property on its model is of type dynamic.

Instead I've tried to figure out how to just pass the whole Model to the next shape:

@if (HasMultipleResults())
{
    var shapeFactory = (IShapeFactory)New;
    // this doesn't work
    var multipleResultsShape = (dynamic)shapeFactory.Create("Search_MultipleResults", Model);
    @Display(multipleResultsShape);
    return;
}

I haven't found a way to get it working yet, can anyone educate me please?


Solution

  • If the first argument of the dynamic method is a type, then this will be used as the model.

    New.YourShape(typeof(YourType))

    See this video for the details: https://www.youtube.com/watch?v=gKLjtCIs4GU&feature=youtu.be&t=1127