开发者

Is it mandatory for a ViewModel class to have explicit constructors?

Is it mandatory for a ViewModel class to have explicit constructors?

EDIT 1:

My ViewModels contains domain data and some lookup data as follows:

namespace MvcMultiList.ViewModels
{
    public class Pe开发者_如何学GorsonVM
    {
        public Person Person { get; set; }
        public MultiSelectList MultiSelectList { get; set; }
    }
}

Does model binding still work without explicit ctors?


No, default parameterless is fine.

You can use object initializer to populate it:

return View(new MyViewModel
{
   Property = 'value'
});

EDIT:

Does model binding still work without explicit ctors?

Yes - default model binding uses parameterless constructors anyway.

If you do not declare any constructor for the class ie. class Foo { } C# compiler will generate a default one for you class Foo { public Foo(){} }.


If you are using the standard modelbinding functionality you must have a parameterless constructor on your view models, you can have other constructor overloads as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜