public static MyViewModel CreateViewModel(Test entity)
{
return new MyViewModel
{
Date = entity.Date,
IsSelected = entity.IsSelected
};
}
readonly MapViewModel _parent;
public MyViewModel (MapViewModel parent)
{
_parent = parent;
}
我收到以下错误消息,我无法弄清楚如何处理它。如果有人有任何想法,我会很高兴。
There is no argument given that corresponds to the required formal parameter 'parent' of 'MyViewModel.MyViewModel(MapViewModel)'.
最佳答案
默认情况下,如果您没有为您的 MyViewModel
类显式指定构造函数,它将假定将使用无参数构造函数。但是,如果您像现在一样只有一个 View 模型,则还需要显式创建一个无参数 View 模型(因为 MVC 只知道需要 parent
参数的 View 模型):
public MyViewModel()
{
// Example of parameterless constructor
}
public MyViewModel (MapViewModel parent)
{
_parent = parent;
}
无参数构造函数也非常有助于 MVC 了解如何序列化内容并将其绑定(bind)到您的模型。
关于c# - 没有给出对应于 x 的所需形式参数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36723736/