开发者

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

开发者 https://www.devze.com 2023-02-10 07:54 出处:网络
Is it mandatory for a ViewModel class to have explicit constructors? EDIT 1: My ViewModels contains domain data and some lookup data as follows:

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

精彩评论

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