casillas
casillas

Reputation: 16793

There is no argument given that corresponds to the required formal parameter of x

public static MyViewModel CreateViewModel(Test entity)
{
    return new MyViewModel
    {
       Date = entity.Date,
       IsSelected = entity.IsSelected
     };
}

readonly MapViewModel _parent;

public MyViewModel (MapViewModel parent)
{
  _parent = parent;
}

I am getting the following error message, I could not able to figure out how to handle that. I would be glad if anyone has any ideas.

There is no argument given that corresponds to the required formal parameter 'parent' of 'MyViewModel.MyViewModel(MapViewModel)'.

Upvotes: 1

Views: 2850

Answers (2)

Rion Williams
Rion Williams

Reputation: 76547

By default, if you don't explicitly specify a constructor for your MyViewModel class, it will assume that a parameter-less one will be used. However if you have a single view model as you currently do, you will need to explicitly create a parameter-less one as well (as MVC only knows of the one that expects a parent parameter) :

public MyViewModel()
{
     // Example of parameterless constructor
}

public MyViewModel (MapViewModel parent)
{
      _parent = parent;
}

Parameter-less constructors can also be incredibly helpful for MVC to know how to serialize content and bind it to your models as well.

Upvotes: 2

Xiaoy312
Xiaoy312

Reputation: 14477

Your MyViewModel doesn't have a parameterless constructor.

public static MyViewModel CreateViewModel(Test entity, MyViewModel parent = null)
{
    return new MyViewModel(parent)
    {
       Date = entity.Date,
       IsSelected = entity.IsSelected
     };
}

Upvotes: 2

Related Questions