Why does the ASP.Net MVC model binder bind an empty JSON array to null?

前端 未结 7 2055
误落风尘
误落风尘 2020-12-15 15:52

Here is my model class:

public class MyModel
{
    public Employees[] MyEmpls{get;set;}
    public int Id{get;set;}
    public OrgName{get;set;}
}

相关标签:
7条回答
  • 2020-12-15 16:55

    you can define a setter that checks if the value is null

    public class MyModel
    {
        private _myEmpls{get;set;}
        public Employees[] MyEmpls{
         get{return _myEmpls;}
         set{_myEmpls=(value==null?new List<Employees>():value);}
        }
    
        public int Id{get;set;}
        public OrgName{get;set;}
    }
    
    0 讨论(0)
提交回复
热议问题