1

We would like to use multiple models in Sitecore SXA view like MVC. Is it possible? We have one custom model and one default model.

Models :

  1. Custom model
  2. @model Sitecore.XA.Foundation.Mvc.Models.RenderingModelBase

How can we use both model in single view?

1
  • 1
    Why don't you make your Custom Model inherit the RenderingModelBase?
    – Mark Cassidy
    May 25 at 13:50
3

You should have your custom model inherit from RenderingModelBase. Something like:

namespace MyCustomApp.Models
{
   public class MyCustomModel : RenderingModelBase
   {
       public string MyField { get; set; }
   }
}

Then, in your view, you would have:

@model MyCustomApp.Models.MyCustomModel
2

It is possible, but not how you are thinking. Mark gave you the answer in the comment, but I'll add it here for completeness.

If you want a custom view model, but still want all the properties/functionality from the base model, you simply inherit from the base model when creating your custom model. e.g:

public class MyCustomViewModel : Sitecore.XA.Foundation.Mvc.Models.RenderingModelBase {
    // add your properties here
}

Now in your razor view, just reference your custom model

@model MyFeature.ViewModels.MyCustomModel

You will probably want to follow the SXA pattern for your controller, and inherit from SXA's set of controllers, VariantsController for variant components, StandardController for others etc... This will allow you to use the repository pattern that SXA uses and let the SXA code to fill the base properties of the base model.

Some clarification on building an SXA compatible controller rendering.

  • You should make sure your controller inherits from the required SXA controller, e.g. VariantsController or PageListController.

  • You do not need to override the Index action, but you should override the GetModel method. This will call your custom repository to populate your model

  • Create a MyCustomRepository that inherits the corresponding SXA repository e.g. VariantsRepository, PageListRepository

  • Override the IRenderingModelBase GetModel() method and add your code to populate your custom model:

      public override IRenderingModelBase GetModel()
      {
          var model = new MyCustomViewModel();
          FillBaseProperties(model);
          model.MyProperty = "Some Value";
          return model;
      }
    

This will make sure that your model being passed back to your view is of the type you expect.

4
  • I am getting this error "The model item passed into the dictionary is of type 'Sitecore.XA.Foundation.Mvc.Models.RenderingModelBase', but this dictionary requires a model item of type 'custommodel.Result'." after inheriting RenderingModelBase to custom model and use custom model namespace in view
    – User7864
    May 25 at 14:25
  • What model is your controller action passing back to the view?
    – Richard Seal
    May 25 at 14:33
  • Well that's your problem then. The model you set in your view has to match the model set from your controller. You need to update your controller. How are you expecting your properties on your custom view model to get filled?
    – Richard Seal
    May 25 at 14:44
  • I have updated the answer to include setting the correct model via your controller/repository
    – Richard Seal
    May 25 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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