0

I am having a base model as:

namespace MyNamespace
{
  public abstract class ViewModelBase
  {
      public string Environment { get; set; }
  }
}

I have 2 other models as:

namespace MyNamespace
{
 public class ErrorViewModel : ViewModelBase
 {
     public string Error { get; set; }
  }
}

namespace MyNamespace
{
 public class SetPageViewModel : ViewModelBase
 {
     public string Name {get; set;}
     public string Address { get; set; }  
 }
}

I am using this in my controller as:

    public IActionResult Index()
    {
        var tuple = new Tuple<ErrorViewModel, SetPageViewModel>(new ErrorViewModel(), new SetPageViewModel ());          

      //  tuple.Item1.Environment = "Dev";
        return View(tuple);
    }

With the above settings I want to have the below in my view to access these models:

    @model Tuple<ErrorViewModel, SetPageViewModel>

But I keep on getting the error:

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Tuple`2[MyNamespace.ErrorViewModel,MyNamespace.SetPageViewModel]', but this ViewDataDictionary instance requires a model item of type 'MyNamespace.ViewModelBase'.

I have tried the following:

  • Commented out the view part where I define my tuple to see if the error goes but no, I still get this error.

  • For both models I tried to remove ViewModelBase ie not to inherit from the base model but I still the above error.

Not sure what else am I missing here.

Leonardo Henriques
  • 736
  • 1
  • 7
  • 18
kaka1234
  • 630
  • 1
  • 8
  • 26
  • 4
    Why even use a Tuple? Why not create a view model which contains your ErrorViewModel and SetPageViewModel? – Ryan Wilson Mar 12 '18 at 12:31
  • Do **any** of your views (including partial) use `@model MyNamespace.ViewModelBase'`? – mjwills Mar 12 '18 at 12:31
  • @mjwills no not the controller/view where I am defining this. I am using ViewModelBase in other views which are not related to this. – kaka1234 Mar 12 '18 at 12:33
  • 1
    I'll bet you a small sum of money one of them is being used in this request. Perhaps as a partial view. _Delete all of the views using `ViewModelBase` and run the code again - it will likely complain about a missing view, and that is your offender._ – mjwills Mar 12 '18 at 12:34
  • @RyanWilson No ryan I have not tried that. I thought creating extra view might be an extra effort rather than just using the above with few lines of code. – kaka1234 Mar 12 '18 at 12:34
  • 1
    @kaka1234 You don't have to create another View, just create a class (ViewModel) that contains both of the other Model classes, then pass that to your View. – Ryan Wilson Mar 12 '18 at 12:44
  • @kaka1234 , check this link once, it may help u: https://stackoverflow.com/questions/7222533/polymorphic-model-binding/7222639#7222639 – VSS Mar 12 '18 at 12:56
  • @RyanWilson Thanks I have used single view model containing those 2 models. And I was still having the same error when using a single model with multiple models in that. I had inherit all from the viewmodelbase to have it working. – kaka1234 Mar 12 '18 at 13:19
  • @mjwills I have too many view using viewmodelbase (though not related to this one) so cant delete those, have to comment lot of code. I have sorted this out using single model having multiple models for now. – kaka1234 Mar 12 '18 at 13:20

0 Answers0